Drives and python

Last Update: 04.12.2006. By kerim in python

How would you scan all drives for files ? Any unix system has an advantage here .... it starts with “". We simply don’t care for drives there. But what do you do if you have windows ?

#!/usr/bin/env python  
# -*- coding: latin-1 -*-  
import sys, os  
def listDrives():  
    drives = []  
    if sys.platform == 'win32':  
        for i in range(ord('a'), ord('z')+1):  
            drive = chr(i)  
            if(os.path.exists(drive +":\\")):  
                drives.append(drive+":\\")  
        return drives

For those that do not mind plattform specific bindings there is an easier way to do this. You need the win32 extensions from here. Once installed you can call:

def listDrivesWin32ExtensionVersion():  
    if sys.platform == 'win32':  
        import win32api,string  
        drives=win32api.GetLogicalDriveStrings()  
        drives=string.splitfields(drives,'\000')  
        return drives

Be careful as this returns a “None” if you are on a non-Windows system.
Nonetheless .... easy and straight forward, wouldn`t you say ?

I guess i should remove the first two drives (a,b) as they are normally associated with fdds. Or at least i should make it optional. Also a good idea would be to make it optional to look for cd/dvd- and networkdrives.

It will come handy when trying to analyze the files on ones’ PC.