1. Operation directory and file name
#!/usr/bin/env python#-*-coding:utf-8-*-import os,reimport shutil Import Time
Search by Listdir
def search_ofd_old (My_pattern, diretory): try: names = Os.listdir (diretory) except Os.error: print " Error ' return for name ' in Names: fullname = Os.path.normpath (Os.path.join (diretory, name)) if Os.path.isfile (FullName): result = My_pattern.search (name) if result and Name.lower (). EndsWith ("TXT"): shutil.copy (FullName, Dest_dir) elif Os.path.isdir (fullname): search_ofd (My_pattern, FullName)
Search using the Walk function
def search_ofd (My_pattern, diretory): For root,dirs,files in Os.walk (diretory): for filename in files: result = My_pattern.search (filename) if result and Filename.lower (). EndsWith ("TXT"): fullname = Os.path.join ( root, filename) shutil.copy (FullName, Dest_dir)
Directory does not exist, it is created:
If not Os.path.isdir (dest_dir): os.makedirs (Dest_dir)
Match Name
Import Repattern = Re.compile ("1ABC") Pattern.search (Var)