search files in all drives using Python

On Windows, you will be better off using the os.walk function. os.walk returns a generator that recursively walks the source tree. The sample below shows a regular expression search.

import os
import re
import win32api

def find_file(root_folder, rex):
    for root,dirs,files in os.walk(root_folder):
        for f in files:
            result = rex.search(f)
            if result:
                print os.path.join(root, f)
                break # if you want to find only one

def find_file_in_all_drives(file_name):
    #create a regular expression for the file
    rex = re.compile(file_name)
    for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
        find_file( drive, rex )


find_file_in_all_drives( 'myfile\.doc' )

Some notes:

  1. I'm using a regular expression for searching the file. For this, I'm compiling the RE ahead of time and then pass it as an argument. Remember to normalize the expression - especially if the file name is coming from a malicious user.
  2. win32api.GetLogicalDriveStrings returns a string with all drivers separated by 0. Split it and then slice out the last element.
  3. During the walk, you can remove unwanted folders from 'dirs' such as '.git' or '.cvs'. See os.walk.__doc__, for example.
  4. To keep the sample short, I did not propagate 'found'. Remove the break if you want to print all files. Propagate the break to find_file_in_all_drives if you want to stop after the first file has been found.

import os
az = lambda: (chr(i)+":\\" for i in range(ord("A"), ord("Z") + 1))
for drv in az():
    for root, dirs, files in os.walk(drv):
        process_the_stuff()