detect if usb is plugged in python code example
Example 1: detect if usb is plugged in python
import pyudev
context = pyudev.Context()
monitor = Monitor.from_netlink()
monitor.filter_by(susbsytem='usb')
monitor.filter_by(susbystem='tty')
for action, device in monitor:
vendor_id = device.get('ID_VENDOR_ID')
if vendor_id in ['22fa']:
print('Detected {} for device with vendor ID {}'.format(action, vendor_id))
Example 2: detect if usb is plugged in python
import pyudev
context = pyudev.Context()
monitor = Monitor.from_netlink()
monitor.filter_by(susbsytem='usb')
monitor.filter_by(susbystem='tty')
for action, device in monitor:
vendor_id = device.get('ID_VENDOR_ID')
if vendor_id in ['22fa']:
print 'Detected {} for device with vendor ID {}'.format(action, vendor_id)