How to check if disk is on disk using python?

Let's say I want to manipulate some files on a floppy disk or a USB card reader. How to check if this disk is ready? (That is, a disk is physically inserted.)

A drive letter exists, so os.exists () will always return True in this case. In addition, at this stage of the process, I still do not know any file names, so checking if this file exists will also not work.

Some clarification: here is the problem of handling exceptions. Most win32 API requests just throw an exception when you try to access a drive that is not ready. This will usually work fine - find something like free space, and then catch the raised exception and assume that this means there is no disk. However, even when I catch all and all exceptions, I still get an angry window with exceptions from Windows saying that the card reader / card reader is not ready. So, I assume that the real question is: how can I suppress a window with Windows errors?

+4
source share
5 answers

And the answer, as with many things, is in an article on C ++ / Win32 programming from decades ago .

In short, the problem is that Windows handles floppy disk errors somewhat differently than other disk errors. By default, no matter what you do, or think it does, Windows intercepts any errors created by the device and provides the user with a dialog box rather than letting the program handle it - the exact problem I had.

But, as it turned out, to solve this problem there is a call to the Win32 API, primarily SetErrorMode()

In a nutshell (and I detail the details here), we can use SetErrorMode() to make Windows stop being so paranoid, do something 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 is probably a Keyser Soze joke here, but today I had the wrong amount of caffeine to find.)

Adapting the C ++ example code from a related article that looks something 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 

In C ++, when detecting errors, the correct path needs the GetLastError () function, which, fortunately, we do not need to worry about, because this is a Python issue. In our case, Python exception handling works fine. Thus, this is a function that I knocked together to check the drive letter for “ready”, all ready for copying, 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 have been using this quite a bit in the last few days, and it works great for both floppy disks and USB card readers.

A few notes: almost any function that requires access to the disk will work in the try block - everything that we look for in the exception due to the lack of media.

Also, although the python win32api package provides all the functions we need, it does not seem to have any flag constants. After a trip to the ancient bowels of MSDN, it turns out that SEM_FAILCRITICALERRORS is 1, which makes our life terribly easy.

I hope this helps someone else with a similar problem!

+7
source

You can compare len(os.listdir("path")) with zero to see if there are any files in the directory.

+4
source

If you have pythonwin, does any information help in this recipe ?

Under the assumption that “Accessibility” and “Status” may be worth a look. Or you can check the name of the volume, which, I think, will be either "X:" or "if there is nothing in the driver." Or, hell, find free space or the total number of blocks.

+1
source

You can use win32 functions through excellent pywin32 ( http://sourceforge.net/projects/pywin32/ ) for this purpose.

I suggest looking at the GetDiskFreeSpace function. You can check the free space on the target disk and continue based on this information.

Alternatively, you can view directory or file changes using the ReadDirectoryChangesW function. You will receive notifications of file changes, etc. But you have to check if this works for you or not. You can look at this foryourself example: http://timgolden.me.uk/python/downloads/watch_directory.py

+1
source

Not sure about your platform, but SNMP might be your answer.

0
source

All Articles