I have created a program to search for a file in the computer. But I feel it can be improved. Can anyone help me with this?filesearch.pyimport win32apiimport osimport iofrom fuzzywuzzy import processdef filesearch(self,*args):    drives = win32api.GetLogicalDriveStrings()    drives = drives.split('\000')[:-1]    searchfile = ''.join((args))    print(Searching...)    try:        q = io.open(index.txt, r, encoding=utf-8)# I have created an index using index.py        x = q.readlines()        z = searchfile.lower()        matches=[]        basenames=[]        for line in x:            ##################################            # I created this part so that if the file doesn't exist it skips the file. But I feel this doesn't work properly.            #if os.path.exists(line) == False:                #pass            ##################################            if z in os.path.basename(line):                matches.append((line[:-1]))        for match in matches:            basenames.append(os.path.basename(match))        s=process.extractOne(z, basenames)        s=list(s)        for item in matches:            if s[0] == os.path.basename(item):                print(Chosen file: %s%(item) + \nMatch percent: %s%(s[1]))                print (item)                try:                    os.startfile(item)                    break                except WindowsError:                    printSir, I am not able to open the file.        print(\nMatches:)        for match in matches:            print(match)        q.close()    # Ih created this part as a failsafe just in case the index isn't up to date    except TypeError, e:        print(e)        searchfile = re.compile(searchfile,re.I|re.DOTALL)        matches = []        for drive in drives:            if 'C' in drive:                for folderName, subfolders, filenames in os.walk(drive):                    # Skip the windows folder as it has to may files and doesn't have anything the user might need.                    if 'Windows' in folderName:pass                    else:                        for filename in filenames:                            thefile = os.path.join(folderName,filename)                            mo = searchfile.search(thefile)                            try:                                #Update index in case the file didn't exist in the index.                                matches.append(thefile)                                q = io.open(index.txt, a, encoding=utf-8)                                q.write((\n+thefile).lower())                                q.close()                            except AttributeError:                                pass            else:                for folderName, subfolders, filenames in os.walk(drive):                    for filename in filenames:                        thefile = os.path.join(folderName,filename)                        mo = searchfile.search(thefile)                        try:                            #Update index in case the file didn't exist in the index.                            matches.append(thefile)                            q = io.open(index.txt, a, encoding=utf-8)                            q.write((\n+thefile).lower())                            q.close()                        except AttributeError:                            pass            if len(matches) != 0:                print\nMatches:                for m in matches:                    print(\n+m)index.pyfrom __future__ import unicode_literalsimport os, sysimport osimport iox = open(index.txt, w)x.write()x.close()from os.path import joinfor (dirname, dirs, files) in os.walk('C:\\'):for filename in files:   thefile = os.path.join(dirname,filename)   print thefile   x = io.open(index.txt, a, encoding=utf-8)   x.write((\n+thefile).lower())   x.close()for (dirname, dirs, files) in os.walk('D:\\'):for filename in files:   thefile = os.path.join(dirname,filename)   print thefile   x = io.open(index.txt, a, encoding=utf-8)   x.write((\n+thefile).lower())   x.close()for (dirname, dirs, files) in os.walk('E:\\'):for filename in files:   thefile = os.path.join(dirname,filename)   print thefile   x = io.open(index.txt, a, encoding=utf-8)   x.write((\n+thefile).lower())   x.close()Also, I believe my program uses depth first search. Is there a better way to search for files on a computer?
File search program not efficient enough
python;python 3.x;search;depth first search
A few points:Your program is neither breadth or depth-first; it uses whatever order os.walk returns.In index.py, opening and closing the index file so often when writing it is slow.  Instead, put the open and close outside the for loop; also, use the with syntax for auto-closing, something like:In index.py, you're repeating code a lot. Consolidate it.from itertools import chaindef drives():    for drv in C, D, E:        yield from os.walk({}:\\.format(drv))with io.open(index.txt, a, encoding=utf-8) as x:    for dirname, dirs, files in drives():        for filename in files:            thefile = os.path.join(dirname,filename)            print thefile            x.write((\n+thefile).lower())