pedro_javierf
Posts: 11
Joined: Fri Nov 06, 2015 4:47 pm

Detect USB connections from Python

Thu Dec 03, 2015 4:22 pm

Helo everybody,

I am working on a project that requires detecting if a new Usb storage has been pluged in. In a first moment I installed PyUSB (And one of its requiered low level usb libs like usblib) but then I noticed that this lib is quite complex just for the simple task I want to do.

In fact, I would like to detect the usb connecteds, and the new ones that could be connected and then browse to the root of the usb for example and list all its files.

Thanks for your help.
My best regards,

EDIT: I've just wondered a better way to do it. My script starts on boot so It could list the connected usbs on boot, and then, each 10 seconds for example check again connected usbs, if its modified from the original listing, I'll know that something new has been inserted, more or less. Any ideas of how to list connected usb devices?

pedro_javierf

Aydan
Posts: 729
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Detect USB connections from Python

Thu Dec 03, 2015 4:32 pm

There's a udev library for python.

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Detect USB connections from Python

Thu Dec 03, 2015 4:39 pm

Aydan wrote:There's a udev library for python.
I've used this to detect CDs being inserted into drives.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

pedro_javierf
Posts: 11
Joined: Fri Nov 06, 2015 4:47 pm

Re: Detect USB connections from Python

Thu Dec 03, 2015 5:26 pm

Thanks for your reply,

I've installed pyudev but I cant list the usbs. I've looked every example but...

Other thing I noticed is that, I'm using raspbian, every usb is mounted in /media/<usblabel> ---> could I check that folder to see if any new usb has been connected?

Thanks!

Aydan
Posts: 729
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Detect USB connections from Python

Fri Dec 04, 2015 1:13 pm

With libudev you don't list usb's you get notified on connect, disconnect events.

This is what you asked for:
pedro_javierf wrote:Helo everybody,

I am working on a project that requires detecting if a new Usb storage has been pluged in.

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Detect USB connections from Python

Fri Dec 04, 2015 2:17 pm

pedro_javierf wrote:. Any ideas of how to list connected usb devices?

pedro_javierf
Rather crude but you could call the ls command to put the names in a file.

On my setup usbs appear in /media/pi

Code: Select all

import subprocess
import os
rpistr = "ls /media/pi > usbs.txt"
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
then read usbs.txt

Aydan
Posts: 729
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Detect USB connections from Python

Thu Dec 10, 2015 3:56 pm

That is the "wood hammer method". to list directory contents in python use os.listdir(path)

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Detect USB connections from Python

Thu Dec 10, 2015 4:20 pm

Aydan wrote:That is the "wood hammer method". to list directory contents in python use os.listdir(path)
Doesn't os.listdir(path) list files in a named directory? How is the OP going to know the path name if inserting a usb stick?

They can use it to list files once they have the USB name.

Aydan
Posts: 729
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Detect USB connections from Python

Fri Dec 11, 2015 12:24 pm

gordon77 wrote:
Aydan wrote:That is the "wood hammer method". to list directory contents in python use os.listdir(path)
Doesn't os.listdir(path) list files in a named directory? How is the OP going to know the path name if inserting a usb stick?

They can use it to list files once they have the USB name.
It lists files and folders in a given path, just like "ls" does.

Return to “Python”