I have a function can list out all the computer connection with USB port.
I want to detect the last/new USB I just plug in, then print out it's txt file from last/new USB I just plug.
One way I come up is that the function can run time1
, before I plug USB in. Then there are 10 devices.
After I plug in, time2
, there are 11 devices. So I can differentiate the last one.
I read through relate question, Trigger new plugged USB-HDD or USB-Stick device but still not sure how to do.
- function can list out all the computer connection with USB port:
import usb.core
import usb.backend.libusb1
print('idVendor idProduct Manufacturer - Product')
print('----------------------------------------------------')
busses = usb.busses()
for bus in busses:
for dev in bus.devices:
if dev:
xdev = usb.core.find(idVendor=dev.idVendor, idProduct=dev.idProduct)
if xdev._manufacturer is None:
xdev._manufacturer = usb.util.get_string(xdev, xdev.iManufacturer)
if xdev._product is None:
xdev._product = usb.util.get_string(xdev, xdev.iProduct)
print('%8d %9d %s - %s' % (dev.idVendor, dev.idProduct,
str(xdev._manufacturer).strip(),
str(xdev._product).strip()))
output of all USB:
idVendor idProduct Manufacturer - Product
----------------------------------------------------
1133 49271 Logitech - USB Optical Mouse
1423 37728 - USB Reader 34148 4096 JetFlash - Mass Storage Device
7531 2 Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
7531 3 Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
5117 2112 Generic - External
7531 2 Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
7531 3 Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller