0

I wrote up a function in Python that looks for a USB drive based on a key identifier file, however when called upon it returns 'There is no disk in the drive. Please insert a disk into drive D:/' (which is an SD card reader) - is there a way to have it search drive letters based on drives that are 'ready'?

def FETCH_USBPATH():
    for USBPATH in ascii_uppercase:
        if os.path.exists('%s:\\File.ID' % USBPATH):
            USBPATH='%s:\\' % USBPATH
            print('USB is mounted to:', USBPATH)
            return USBPATH + ""
    return ""

USBdrive = FETCH_USBPATH()
while USBdrive == "":
    print('Please plug in USB & press any key to continue', end="")
    input()
    FlashDrive = FETCH_USBPATH()

Had a fix in cmd here however turned out command-prompt based didn't suit my needs.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Julian White
  • 1,603
  • 3
  • 12
  • 12
  • See http://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python to list available drive letters. I'm not sure if the method there will return a drive with no disk. – interjay Mar 15 '15 at 10:09
  • Richie's answer worked however that also returned D:// which is an 'empty' drive/ – Julian White Mar 15 '15 at 10:17
  • You can use ctypes to call [`SetThreadErrorMode`](https://msdn.microsoft.com/en-us/library/windows/desktop/dd553630%28v=vs.85%29.aspx). Setting the mode to `SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS` should prevent CSRSS from showing the error box. Restore the old mode after you're done. – Eryk Sun Mar 15 '15 at 11:55
  • @eryksun - I am a coding newbie and would have zero idea on how to accomplish this, where would I get started? – Julian White Mar 15 '15 at 13:00
  • @eryksun - '\\?\Volume{526c17d7-adb8-11e4-81da-806e6f6e6964}\ – Julian White Mar 15 '15 at 14:08
  • @eryksun - I have added your ctypes SetThreadedErrorMode section to my code - I will update the original post - it doesn't seem to do anything unfortunately. – Julian White Mar 15 '15 at 14:14
  • Answers do not belong in the question. You may post your own answer if you like. – nobody Mar 16 '15 at 00:27
  • @AndrewMedico I needed to copy Eryksun's updated/edited code which did actually fix it, but thanks for deleting it before I got the chance to save what he did - can you please restore it temporarily? – Julian White Mar 16 '15 at 10:40
  • @eryksun Can you please repost what you had added to the question, as an answer? Your code snippet works but I didn't save the project - went to retrieve it now to find Andrew had deleted it. Lovely. – Julian White Mar 16 '15 at 10:46
  • @AndrewMedico, sorry I should have provided an answer, but I'm averse to posting an 'answer' that I cannot personally test. I first wanted feedback that it worked, but the OP misunderstood my comment. I had every intention to roll the edit back (and post an answer if it worked); I was just waiting for Julian to notify me. – Eryk Sun Mar 16 '15 at 11:01
  • @JulianWhite, you should be able to see the entire edit history by clicking on the link that shows the last edit time, so technically Andrew didn't delete anything. – Eryk Sun Mar 16 '15 at 11:11
  • Strange, I clicked that but all it managed to do was load my original question. I'm still newish here, as you can probably tell ;) – Julian White Mar 16 '15 at 11:42

1 Answers1

3

Finding 'ready' drives may be more trouble that it's worth for your needs. You can probably get away with just temporarily disabling the error message dialog box via SetThreadErrorMode.

import ctypes

kernel32 = ctypes.WinDLL('kernel32')

SEM_FAILCRITICALERRORS = 1
SEM_NOOPENFILEERRORBOX = 0x8000
SEM_FAIL = SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS

def FETCH_USBPATH():
    oldmode = ctypes.c_uint()
    kernel32.SetThreadErrorMode(SEM_FAIL, ctypes.byref(oldmode))
    try:
        for USBPATH in ascii_uppercase:
            if os.path.exists('%s:\\File.ID' % USBPATH):
                USBPATH = '%s:\\' % USBPATH
                print('USB is mounted to:', USBPATH)
                return USBPATH
        return ""
    finally:
        kernel32.SetThreadErrorMode(oldmode, ctypes.byref(oldmode))

USBdrive = FETCH_USBPATH()
while USBdrive == "":
    print('Please plug in our USB drive and '
          'press any key to continue...', end="")
    input()
    USBdrive = FETCH_USBPATH()
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Ah you're a life saver mate - You're absolutely right, finding 'not-ready' drives and skipping them could use a hunk of time and programming that I frankly lack the time and skill for (coding is obviously not my area). Temp hiding it fixes it, it's nothing serious an error. – Julian White Mar 16 '15 at 11:12