Is device Connected Windows?

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Jun 1 04:23:06 EDT 2006


[placid]

| Just wondering is there a way (not brute force) to check if a usb
| storage device is connected?

Hmmm. How do you identify "a usb storage device" to know that
it is or isn't connected?

You can certainly do something useful with wmi. eg,

<code>
import wmi

c = wmi.WMI ()
for usb_disk in c.Win32_DiskDrive (InterfaceType="USB"):
  print usb_disk.Caption
  print usb_disk.PNPDeviceID

</code>

Now, assuming that the PNPDeviceID is unique enough for
your purpose, you could probably do something with it
to keep hold of it and then check later whether the
same device is still inserted. One possibility is
to use a WMI watcher to spot when devices are removed:

<code>
import wmi

c = wmi.WMI ()
usb_watcher = c.watch_for (
  notification_type="Deletion",
  wmi_class="Win32_DiskDrive",
  delay_secs=2,
  InterfaceType="USB"
)

while True:
  usb_removed = usb_watcher () # can optionally timeout
  print usb_removed.PNPDeviceID

</code>

Lots of other possibilities, a polling loop, two
watchers, one for creation one for deletion etc.
Depends exactly what your requirements are.

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list