Search Files In All Drives Using Python
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 com
Solution 1:
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 thebreak
tofind_file_in_all_drives
if you want to stop after the first file has been found.
Solution 2:
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()
Solution 3:
You need to specify drives for example for c drive.
def findall(directory):
files=os.listdir(directory)
for fl in files:
path=os.path.join(directory,fl)
if os.path.isdir(path):
findall(path)
else:
dosomethingwithfile(path)
return
Basically you traverse file tree. You have to pass drives to this method though as root dir. eg. findall('c:/')
Post a Comment for "Search Files In All Drives Using Python"