Python-implemented example of finding data files by file name

Source: Internet
Author: User
Tags readfile
This article mainly describes the Python implementation based on the file name to find the data file function, involving Python for file and directory traversal, query and other related operations skills, the need for friends can refer to the following

This article describes the Python implementation of the search data file based on the file name feature. Share to everyone for your reference, as follows:

#-*-coding:utf-8-*-import osimport shutilallfiles=[]namefiles=[]def findfie (filePath): Pathdir = Os.listdir (filePath ) for Alldir in Pathdir: # print (Alldir) allfiles.append (alldir) #pass #filepath = ' C:\\users\\ibm_admin\\desktop\\cogno S\\datastage\\71&72\\71\\71sns ' #copyfile = ' c:\\users\\ibm_admin\\desktop\\cognos\\datastage\\71&72\\ 71MTP ' filepath = ' C:\\users\\ibm_admin\\desktop\\cognos\\datastage\\71&72\\72\\72sns ' copyfile = ' c:\\users\\ Ibm_admin\\desktop\\cognos\\datastage\\71&72\\72mtp ' Shutil.rmtree (copyfile) os.mkdir (copyfile) FindFie ( FilePath) def readFile (): readFile = Open ('./jobname ') i = 0 for eachline in readfile:i= i + 1 #print (eachline) Namefil Es.append (eachline.replace (' \ n ', ')) # Remove line break ReadFile () #字符串比较def dothecompare (): For x in Namefiles:print (x) for y in Al Lfiles:if x = = Y:copyfrom = Os.path.join (filepath,x) copyTo = Os.path.join (copyfile,x) shutil.copyfile (COPYF Rom,copyto) Else:pass #print ("File not find under SNS ProCess,thanks. Please check with Wumi. ") Dothecompare ()

attached: Here is an example of a simpler file search feature:

#-*-Coding:utf-8-*-import osdef Search (path= ".", Name= "1"): For  item in Os.listdir (path):    Item_path = Os.path. Join (path, item)    if Os.path.isdir (item_path):      search (Item_path, name)    elif os.path.isfile (item_path):      if name in item:        print (item_path) if __name__ = = "__main__":  Search (path=r "D:\360Downloads", name= "DLL")

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.