9
votes

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

3
Please try it yourself first, then ask for any particular problems that you couldn't overcome. - manas
Could you be more precise on your need, what is your OS, what do you mean by all drives: all local drives, network shared disk? - FabienAndre
@silent_warrior see edit question - user1223495
@FabienAndre see edit question - user1223495

3 Answers

10
votes

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.
2
votes
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()
1
votes

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:/')