Re: Eject a Removable USB drive

Discussion in 'Python' started by Mark Hammond, Mar 9, 2009.

  1. Mark Hammond

    Mark Hammond Guest

    On 10/03/2009 8:20 AM, Rickey, Kyle W wrote:
    > Thanks for the link! That code has got me on the right track. I've
    > almost got it working with one small kink.
    >
    > After the code runs my drive still shows up on Windows Explorer but as a
    > removable drive. If I try to double click on it, it tells me to insert a
    > disk (see screenshot).
    >
    > So it seems my code is unmounting my volume, but not the actual device.
    > Any ideas? I've attached the code I've got so far.


    Adding the following after your eject code runs:

    from win32com.shell import shell, shellcon
    shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH,
    "F:\\")


    seems to work for me (well - I actually did the above interactively
    after your code ran, and the disk magically vanished from explorer...)

    Cheers,

    Mark
     
    Mark Hammond, Mar 9, 2009
    #1
    1. Advertising

  2. Mark Hammond

    Aaron Brady Guest

    On Mar 9, 6:08 pm, Mark Hammond <> wrote:
    > On 10/03/2009 8:20 AM, Rickey, Kyle W wrote:
    >
    > > Thanks for the link! That code has got me on the right track. I've
    > > almost got it working with one small kink.

    >
    > > After the code runs my drive still shows up on Windows Explorer but as a
    > > removable drive. If I try to double click on it, it tells me to insert a
    > > disk (see screenshot).

    >
    > > So it seems my code is unmounting my volume, but not the actual device.
    > > Any ideas? I've attached the code I've got so far.

    >
    > Adding the following after your eject code runs:
    >
    > from win32com.shell import shell, shellcon
    > shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH,
    > "F:\\")
    >
    > seems to work for me (well - I actually did the above interactively
    > after your code ran, and the disk magically vanished from explorer...)


    Yay, magically! import crystalball?
     
    Aaron Brady, Mar 9, 2009
    #2
    1. Advertising

  3. def do_magic():
    from staples import easy_button
    result = easy_button.press()
    return result

    :) In all seriousness that code did the trick but only after a short delay. I noticed when I first ran it, there was no effect. But when I ran it interactively, it succeeded.

    Is there any way to check that the drive is still showing up in explorer and then re-run the code? Something like:

    while drive_exists:
    shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH, "F:\\")


    Right now I've got:

    time.sleep(1)
    shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH, "F:\\")


    Thanks for your help!

    -Kyle Rickey

    -----Original Message-----
    From: python-list-bounces+kyle.rickey= [mailto:python-list-bounces+kyle.rickey=] On Behalf Of Aaron Brady
    Sent: Monday, March 09, 2009 6:15 PM
    To:
    Subject: Re: Eject a Removable USB drive

    On Mar 9, 6:08 pm, Mark Hammond <> wrote:
    > On 10/03/2009 8:20 AM, Rickey, Kyle W wrote:
    >
    > > Thanks for the link! That code has got me on the right track. I've
    > > almost got it working with one small kink.

    >
    > > After the code runs my drive still shows up on Windows Explorer but as a
    > > removable drive. If I try to double click on it, it tells me to insert a
    > > disk (see screenshot).

    >
    > > So it seems my code is unmounting my volume, but not the actual device.
    > > Any ideas? I've attached the code I've got so far.

    >
    > Adding the following after your eject code runs:
    >
    > from win32com.shell import shell, shellcon
    > shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH,
    > "F:\\")
    >
    > seems to work for me (well - I actually did the above interactively
    > after your code ran, and the disk magically vanished from explorer...)


    Yay, magically! import crystalball?
    --
    http://mail.python.org/mailman/listinfo/python-list
     
    Rickey, Kyle W, Mar 10, 2009
    #3
  4. Mark Hammond

    Mark Hammond Guest

    On 11/03/2009 12:39 AM, Rickey, Kyle W wrote:
    > def do_magic():
    > from staples import easy_button
    > result = easy_button.press()
    > return result
    >
    > :) In all seriousness that code did the trick but only after a short delay. I noticed when I first ran it, there was no effect. But when I ran it interactively, it succeeded.
    >
    > Is there any way to check that the drive is still showing up in explorer and then re-run the code? Something like:


    It is possible to register for device removal notifications - it's
    possible you need to wait until Windows reports the ejection process is
    complete before sending the shell notification.

    Cheers,

    Mark

    >
    > while drive_exists:
    > shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH, "F:\\")
    >
    >
    > Right now I've got:
    >
    > time.sleep(1)
    > shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH, "F:\\")
    >
    >
    > Thanks for your help!
    >
    > -Kyle Rickey
    >
    > -----Original Message-----
    > From: python-list-bounces+kyle.rickey= [mailto:python-list-bounces+kyle.rickey=] On Behalf Of Aaron Brady
    > Sent: Monday, March 09, 2009 6:15 PM
    > To:
    > Subject: Re: Eject a Removable USB drive
    >
    > On Mar 9, 6:08 pm, Mark Hammond<> wrote:
    >> On 10/03/2009 8:20 AM, Rickey, Kyle W wrote:
    >>
    >>> Thanks for the link! That code has got me on the right track. I've
    >>> almost got it working with one small kink.
    >>> After the code runs my drive still shows up on Windows Explorer but as a
    >>> removable drive. If I try to double click on it, it tells me to insert a
    >>> disk (see screenshot).
    >>> So it seems my code is unmounting my volume, but not the actual device.
    >>> Any ideas? I've attached the code I've got so far.

    >> Adding the following after your eject code runs:
    >>
    >> from win32com.shell import shell, shellcon
    >> shell.SHChangeNotify(shellcon.SHCNE_DRIVEREMOVED, shellcon.SHCNF_PATH,
    >> "F:\\")
    >>
    >> seems to work for me (well - I actually did the above interactively
    >> after your code ran, and the disk magically vanished from explorer...)

    >
    > Yay, magically! import crystalball?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Mark Hammond, Mar 10, 2009
    #4
    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. David Goulet

    Removable Drive

    David Goulet, Jan 20, 2004, in forum: Java
    Replies:
    2
    Views:
    396
    Christophe Vanfleteren
    Jan 20, 2004
  2. Prakru
    Replies:
    1
    Views:
    768
    Karthik
    May 14, 2004
  3. Mike Joyce
    Replies:
    3
    Views:
    3,166
    Gordon Rainsford
    May 3, 2006
  4. shailesh
    Replies:
    1
    Views:
    833
    Tim Golden
    Mar 28, 2007
  5. Replies:
    1
    Views:
    1,037
    Jim Langston
    Jan 12, 2009
Loading...

Share This Page