Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 269123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:47:13+00:00 2026-05-11T23:47:13+00:00

Say I want to manipulate some files on a floppy drive or a USB

  • 0

Say I want to manipulate some files on a floppy drive or a USB card reader. How do I check to see if the drive in question is ready? (That is, has a disk physically inserted.)

The drive letter exists, so os.exists() will always return True in this case. Also, at this point in the process I don’t yet know any file names, so checking to see if a given file exists also won’t work.

Some clarification: the issue here is exception handling. Most of the win32 API calls in question just throw an exception when you try to access a drive that isn’t ready. Normally, this would work fine – look for something like the free space, and then catch the raised exception and assume that means there isn’t a disk present. However, even when I catch any and all exceptions, I still get an angry exception dialog box from Windows telling me the floppy / card reader isn’t ready. So, I guess the real question is – how do I suppress the windows error box?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:47:13+00:00Added an answer on May 11, 2026 at 11:47 pm

    And the answer, as with so many things, turns out to be in an article about C++/Win32 programming from a decade ago.

    The issue, in a nutshell, is that Windows handles floppy disk errors slightly differently than other kinds of drive errors. By default, no matter what you program does, or thinks it’s doing, Windows will intercept any errors thrown by the device and present a dialog box to the user rather than letting the program handle it – the exact issue I was having.

    But, as it turns out, there’s a Win32 API call to solve this issue, primarily SetErrorMode()

    In a nutshell (and I’m handwaving a way a lot of the details here), we can use SetErrorMode() to get Windows to stop being quite so paranoid, do our thing and let the program handle the situation, and then reset the Windows error mode back to what it was before as if we had never been there. (There’s probably a Keyser Soze joke here, but I’ve had the wrong amount of caffeine today to be able to find it.)

    Adapting the C++ sample code from the linked article, that looks about like this:

    int OldMode; //a place to store the old error mode
    //save the old error mode and set the new mode to let us do the work:
    OldMode = SetErrorMode(SEM_FAILCRITICALERRORS); 
    // Do whatever we need to do that might cause an error
    SetErrorMode(OldMode); //put things back the way they were
    

    Under C++, detecting errors the right way needs the `GetLastError()’ function, which we fortunately don’t need to worry about here, since this is a Python question. In our case, Python’s exception handling works fine. This, then, is the function I knocked together to check a drive letter for “readiness”, all ready for copy-pasting if anyone else needs it:

    import win32api
    def testDrive( currentLetter ):
        """
        Tests a given drive letter to see if the drive is question is ready for 
        access. This is to handle things like floppy drives and USB card readers
        which have to have physical media inserted in order to be accessed.
        Returns true if the drive is ready, false if not.
        """
        returnValue = False
        #This prevents Windows from showing an error to the user, and allows python 
        #to handle the exception on its own.
        oldError = win32api.SetErrorMode( 1 ) #note that SEM_FAILCRITICALERRORS = 1
        try:
            freeSpace = win32file.GetDiskFreeSpaceEx( letter )
        except:
            returnValue = False
        else:
            returnValue = True
        #restore the Windows error handling state to whatever it was before we
        #started messing with it:
        win32api.SetErrorMode( oldError )
        return returnValue
    

    I’ve been using this quite a bit the last few days, and it’s been working beautifully for both floppies and USB card readers.

    A few notes: pretty much any function needing disk access will work in the try block – all we’re looking for in an exception due to the media not being present.

    Also, while the python win32api package exposes all the functions we need, it dones’t seem to have any of the flag constants. After a trip to the ancient bowels of MSDN, it turns out that SEM_FAILCRITICALERRORS is equal to 1, which makes our life awfully easy.

    I hope this helps someone else with a similar problem!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to have a method that takes any kind of number, is
Say I want to define that an URI such as: myapp://path/to/what/i/want?d=This%20is%20a%20test must be handled
Let's say I saved some .ai file as .svg . Now I want to
I'm currently developing an application that does some file manipulation and I want to
I want to pause the timer thread....or i can say want to put it
Say I want to draw a Ball in the scene and here are two
Say you want to install a library lib-a which has dependencies dep-1 and dep-2
Say I want to write a function like this: int get_some_int(string index) { ...perform
Say I want to disassemble lines m-n of file x, where file x is
say I want to scaffold generate university but university should eventually be related to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.