http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
这篇文章写得很好啊。
另外字符类的正则使用
du -sh .[a-zA-Z]* ~/
ls | grep "^[0-9].*"
##cmd_eg : phthon change.py /home/log
import os
import re
import sys
log_dir=sys.argv[1]#'/home/wren/script/python/test1'
search_argu=[['check_logfiles.log.bz2'],['sf_tc.store','check_logfiles.log'],['<result>FAILED','<item>check_logs failed'],['ipdb_common_sync_glitch_chk']]
change_search_argu=['<result>PASSED','<item>""'] #search_argu[0] is the two search search_arguments in file1 #search_argu[1] is the two search search_arguments in file2
reObj = re.compile('#.*')
#re 编译为pattern
def detect():#ergodic the files in path log_dir #search_argu[1] is the two search search_arguments in file2
i=0
cd_folder= 'cd '+log_dir+' && touch module_log_merge.txt'
# print os.getcwd()
# print ('cd_folder_and_touch')
# os.system(cd_folder)
os.system(cd_folder)
print os.getcwd()
global script_dir
script_dir = os.getcwd()
# f = open('module_log_merge.txt', 'w')
# f.close()
global check_file1
global check_file2
for rt,dirs,files in os.walk(log_dir):#ergodic the files in path log_dir
for file in files:
#detect zip file
print(rt)
path=rt+'/'
pathlist=os.listdir(path)
for filename in pathlist:
if filename.endswith(str(search_argu[0][0])):
print("*******need to bunzip********")
order1='cd '+path+' && bunzip2 '+filename
os.system(order1)
#利用OS.system执行命令
else:
pass
if (pathlist.count(search_argu[1][0])==0) or (pathlist.count(search_argu[1][1])==0):
i+=1
print('file not found')
print(i)
break
try:
check_file1=path+search_argu[1][0]
check_file2=path+search_argu[1][1]
except FileNotFoundError:
print('can not find files')
sys.exit()
merge_log(check_file2)
print ("##################################################")
break
def check1(file):#check file st_tc.store has [<result>FAILED] [<item>check_logs failed] or not
a=0
b=0
#print('check1')
open_file1=open(file)
#print(open_file1)
for line in open_file1:
if(re.search(search_argu[2][0],line)):
a=1
break
for line in open_file1:
if (re.search(search_argu[2][1],line)):
b=1
break
open_file1.close()
if(a+b==2):
print('check1 succeed')
return(1)
else:
print('check1 failed')
return(0)
def merge_log(file):#check file check_logfiles.log
num=0
should_detect=[]
remain_file=[]
open_file2=open(file)
# script_dir = os.getcwd()
if(check1(check_file1)==1):
os.chdir(log_dir)
with open('module_log_merge.txt','r+') as add2txt:
for line in open_file2:
if line.lstrip()[0].isdigit():
filter_num_line = reObj.search(line)
#利用编译的对象,进行search
add2txt.write(filter_num_line.group(0)+'\n')
else:
pass
else:
print('this case is pass')
os.chdir(script_dir)
if __name__=='__main__':
detect()
这个脚本实现的是
1,在目标文件夹中创建一个文本A
2,将目标文件夹中所有的子文件夹中,每个check_logfiles.log的,以#来头的行搜集起来,写到这个创建的文本A中