search files in all drives using Python
python, python-2.7
Solution
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:
- 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.
- `win32api.GetLogicalDriveStrings` returns a string with all drivers separated by 0. Split it and then slice out the last element.
- During the walk, you can remove unwanted folders from 'dirs' such as '.git' or '.cvs'. See `os.walk.__doc__`, for example.
- 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.
Problem
I work with python and I need a function or library that search for my files in all drives, that I give it just the name of files as F3 in Windows that search on all folders in computer. windows os , local drives ,, i write a code ``` import os import win32api paths = 'D:/' def dir_list_folder(paths): for folderName in os.listdir(paths): if (folderName.find('.') == -1): folderPath = os.path.join(paths,folderName ); dir_list_folder(folderPath); else: print ('Files is :'+ folderName ); ``` it give me a good result but some type is give me an error , if i don't need to search in .Zip or .RAR file how i can do that