RE: Is device Connected Windows?

Discussion in 'Python' started by Tim Golden, Jun 1, 2006.

  1. Tim Golden

    Tim Golden Guest

    [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
    ________________________________________________________________________
     
    Tim Golden, Jun 1, 2006
    #1
    1. Advertising

  2. Tim Golden

    placid Guest

    Tim Golden wrote:
    > [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>
    >


    Thanks mate, this is what i was after, i can work my way through here.
     
    placid, Jun 5, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nishadixit
    Replies:
    5
    Views:
    1,298
    Joe Discar
    Jun 1, 2005
  2. placid

    Is device Connected Windows?

    placid, Jun 1, 2006, in forum: Python
    Replies:
    0
    Views:
    303
    placid
    Jun 1, 2006
  3. Replies:
    1
    Views:
    462
    santosh
    Nov 27, 2006
  4. Matt
    Replies:
    0
    Views:
    507
  5. Arihan Sinha
    Replies:
    9
    Views:
    188
    Arihan Sinha
    Oct 28, 2010
Loading...

Share This Page