Reading pen drive data

mukesh tiwari mukeshtiwari.iiitm at gmail.com
Sat Sep 3 10:40:03 EDT 2011


Hello all
I am trying to write a python script which can mount a pen drive and
read the data from pen drive. I am using pyudev [
http://packages.python.org/pyudev/api/index.html ] and wrote a small
python code

import pyudev, sys
if __name__ =="__main__":
        context = pyudev.Context()
        devices = context.list_devices(subsystem        ="usb")
        for device in devices :
                print device

which prints

Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb3')
Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1a.1/usb4')
Device(u'/sys/devices/pci0000:00/0000:00:1a.1/usb4/4-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1a.2/usb5')
Device(u'/sys/devices/pci0000:00/0000:00:1a.2/usb5/5-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1a.7/usb1')
Device(u'/sys/devices/pci0000:00/0000:00:1a.7/usb1/1-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb6')
Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb6/6-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb6/6-2')
Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb6/6-2/6-2:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1d.1/usb7')
Device(u'/sys/devices/pci0000:00/0000:00:1d.1/usb7/7-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1d.2/usb8')
Device(u'/sys/devices/pci0000:00/0000:00:1d.2/usb8/8-0:1.0')
Device(u'/sys/devices/pci0000:00/0000:00:1d.7/usb2')
Device(u'/sys/devices/pci0000:00/0000:00:1d.7/usb2/2-0:1.0')

My problem is,  how to know  out of these usb which one to read . The
other solution I got is whenever I insert the pen drive , my system
mounts  it in /media  directory so I can read the /media directory  to
check if its empty or not but this does not seems promising to me
because it may be possible that my system is not able to mount the pen
drive so I have to manually do it .  Kindly some one please tell me
how to solve this problem.



More information about the Python-list mailing list