How can i get file directory of usb device afer detect it when i placed in raspberry pi in python?

878 views Asked by At

Im using pyudev for detecting when an usb device is placed in raspberry pi. But i need get file directory of it for saving a excel file in an usb after i detect it

Code is:

import pyudev
import RPi.GPIO as GPIO
import time 
from time import sleep
from os import path

#FUNCION PARA DETECTAR USB Y GUARDAR DATOS 
def guardarUSB():
  context = pyudev.Context()
  monitor = pyudev.Monitor.from_netlink(context)
  monitor.filter_by(subsystem='usb')
  Led = 32
  GPIO.setwarnings(False)
  GPIO.setmode(GPIO.BOARD)
  GPIO.setup(Led,GPIO.OUT)
  GPIO.output(Led, GPIO.LOW) #poner la seƱal de salida en alto (High) o bajo (low)
  time.sleep(3)
  GPIO.cleanup() #limpiar el pin
  for device in iter(monitor.poll, None):
     if device.action == 'add':
        print('{} connected'. format(device.sys_path))
guardarUSB()
1

There are 1 answers

0
cs1349459 On

Using the Raspbian OS, which is similar to Linux, external hard drives with be placed in /media/pi. If you use os.listdir, you will be able to find if you have hard drives or not. Maybe use this code to find how many hard drives are in:

import os

how_many_hard_drives = len(os.listdir("/media/pi"))

print(how_many_hard_drives)