Sometimes when you want to find a file, but forget the location of the file in the computer, this is a frequently encountered problem.
Of course, if you use Windows 7, you can search directly with the search box in the upper right corner.
Recently in learning Python, just take this to practice practiced hand, write a script to find files.
The main idea is to traverse all the files and subdirectories in the directory, and compare them to the files to be found, and put them in the search results if they match.
1 ImportOs,sys,pprint,time2 defFind (pattern,directory):3Found =[]#Store The result4Pattern = Pattern.lower ()#Normalize to lowercase5 #print (File_find)6 for(Thisdir,subshere,fileshere)inchos.walk (directory):7 forFileinchFileshere + subshere:#Search All the files and Subdirect8 ifPatterninchfile.lower ():9 found.append (Os.path.join (thisdir,file))Ten returnfound One A if __name__=='__main__': -Directory = input ('Enter directory:\n') -Pattern = input ('Enter filename you search:\n') theT1 = Time.clock ()#Calculate the running time -Found =Find (Pattern,directory) -t2 =Time.clock () - Print(t2-T1) +Pprint.pprint (found[:])#Print The result
Python script to find the location of files on your computer