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")