I'm building a small camera with my raspberry pi zero. As part of this I've made an enclosure with an external USB port, which is hardwired onto the pi.

I would like to add an if statement to the start of my python script to ensure that a USB device is plugged in. The port will never change as it is hardwired, and I am not trying to detect a specific USB stick. Just that the port is occupied.

Further, is there any way to ensure it is a USB storage device?

  • 1
    If you were working in a (bash) shell environment I'd be tempted to do something with lsusb(8) - which suggests the information may also be available from the /proc or /sys parts of the file-system... – SlySven Apr 23 '18 at 12:38
  • 1
    My current method involves using a similar method, I just make the lsusb call from python, capture the result and parse out the desired port. Would prefer a purely python method. – Charlie Miller Apr 23 '18 at 17:35

Test for the existence of /sys/bus/usb/devices/1-1.2 (or 1.3, 1.4, 1.5 which are the numbers of the 4 A-type connectors).

You can also parse the output of lsusb -t. You will note that this also includes the device class, which will be "Mass Storage" for a storage device.

|improve this answer|||||
  • Exactly what I was searching for. Thankyou! – Charlie Miller Apr 27 '18 at 1:54
  • Starting at 1.2 because the hub is 1.0 and the Ethernet is 1.1? I'll try this as soon as I get home from work. Thanks again! – Charlie Miller Apr 27 '18 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.