Python script to find the location of files on your computer

Source: Internet
Author: User
Tags pprint

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.