1. Operation directory and filename
#!/usr/bin/env python
#-*-coding:utf-8-*-
import os,re import
shutil
import Time
Search with Listdir
def search_ofd_old (My_pattern, diretory):
try:
names = Os.listdir (diretory)
except Os.error:
print Error '
return
to 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 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 re Pattern
= Re.compile ("1ABC")
Pattern.search (Var)