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.py
import win32api
import os
import io
from fuzzywuzzy import process
def 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:
print"Sir, 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.py
from __future__ import unicode_literals
import os, sys
import os
import io
x = open("index.txt", "w")
x.write("")
x.close()
from os.path import join
for (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?
index.py
is still off (thefor
loops should be nested). \$\endgroup\$