6


First sorry for my english !

my environement :
python : 2.7.3
wxwidgets : 2.9.4-1
wxpython : 2.9.4-1
ubuntu : 12.04

context :
I have to detect when an usb hard-drive is plugged or unplugged and do some action on it.
For example when a disk is plugged i wan to get the mount point (ex:/media/usb0) and the system point (ex:/dev/sdb1). I need both two path and i do not want made a system call like (subprocess : mount -l).

I have tried several ways :
- pyudev : only get the system path on EVT_DEVICE_ADDED (like /dev/sdb1)
- Gio (gi.repository) : get the mount point with 'mount-added' (like /media/usb0) and the system point in an second event 'volume-added' but i have problems with Gio add and remove event fail or have suspect behavior depends on computer i have tried my application on it
- DBusGMainLoop (dbus.mainloop.glib) : Works but depends on computer i tried it (all on the same configuration) launch 2 event 'DeviceAdded', and sometime one DeviceChanged but sometime not when a disk is plugged.

Do you know a way (maybe one of the 3 i exposed, i have done something bad) to detect when an usb disk is plugged, call a method and in this method get the 2 path i need ?

Thanks in advance.

Aurélien.

1 Answer 1

3

I use this to check attached USB devices:

Requirements

  • pyusb

Example

import usb from usb.core import USBError ### Some auxiliary functions ### def _clean_str(s): ''' Filter string to allow only alphanumeric chars and spaces @param s: string @return: string ''' return ''.join([c for c in s if c.isalnum() or c in {' '}]) def _get_dev_string_info(device): ''' Human readable device's info @return: string ''' try: str_info = _clean_str(usb.util.get_string(device, 256, 2)) str_info += ' ' + _clean_str(usb.util.get_string(device, 256, 3)) return str_info except USBError: return str_info def get_usb_devices(): ''' Get USB devices @return: list of tuples (dev_idVendor, dev_idProduct, dev_name) ''' return [(device.idVendor, device.idProduct, _get_dev_string_info(device)) for device in usb.core.find(find_all=True) if device.idProduct > 2] 

I hope it helps! I have some more code related to USB stuff here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.