Reading pen drive data

MRAB python at mrabarnett.plus.com
Sat Sep 3 19:16:01 CEST 2011


On 03/09/2011 17:21, mukesh tiwari wrote:
> On Sep 3, 7:40 pm, mukesh tiwari<mukeshtiwari.ii... at gmail.com>  wrote:
>> 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.
>
> I got this link and its working fine [ http://packages.python.org/pyudev/api/monitor.html
> ]
>>>> context = pyudev.Context()
>>>> monitor = pyudev.Monitor.from_netlink(context)
>>>> monitor.filter_by(subsystem='input')
>>>> for action, device in monitor:
> ...     print('{0}: {1}'.format(action, device))
>
> but when I am trying to execute code [ http://packages.python.org/pyudev/api/toolkit.html
> ]
>
> import pyudev, sys
>
> context = pyudev.Context()
> monitor = pyudev.Monitor.from_netlink(context)
> monitor.filter_by(subsystem='usb')
> observer = QUDevMonitorObserver(monitor)
> def device_connected(device):
>       print('{0!r} added'.format(device))
> observer.deviceAdded.connect(device_connected)
> monitor.start()
>
> I am getting
> Traceback (most recent call last):
>    File "Mount.py", line 7, in<module>
>      observer = QUDevMonitorObserver(monitor)
> NameError: name 'QUDevMonitorObserver' is not defined
>
> Could any one please tell me how to avoid this error .
> Thank you

I think you need to import it from pyudev.pyqt4.



More information about the Python-list mailing list