使用python递归子目录处理日志文件

重要说明

(1)python使用4个空格进行层次缩进的(不是tab),在eclipse里面可以直接使用tab缩进,是因为eclipse会实时地将tab转成4个空格

(2)在eclipse中安装pyDev插件,就可以调试python脚本了

(3)如果在python文件中存在中文字符,需要在python文件的开头处指明文件的编码类型,形式如:#coding=gbk

(4)以下代码使用的是python2.7.3版本

+++++++++  main.py  +++++++++

#coding=gbk
# filename : main.py
# author : kekec
# date : 20140813 import os,sys
import filedir
import filter file_suffix = '*.txt'
root_path = 'F:\\新建文件夹\\20140714'
result_path = unicode('result.txt' , "utf8")
wfile = open(result_path, 'w')
for i in filedir.search_file(file_suffix, root_path):
print i
bfile = False;
rfile = open(i, 'r')
while 1:
line = rfile.readline()
if not line:
break if (False == filter.is_filter(line)):
if (False == bfile):
wfile.write(i)
wfile.write('\n')
bfile = True
print line
wfile.write(line)
wfile.flush() rfile.close()
wfile.close()

+++++++++  filedir.py  +++++++++

#coding=gbk
# filename : filedir.py
# author : kekec
# date : 20140813 import os,sys,fnmatch def search_file(pattern="*.txt", root=os.curdir):
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)

+++++++++  filter.py  +++++++++

#coding=gbk
# filename : filter.py
# author : kekec
# date : 20140813 ALOG_0 = '[ERROR]'
BLOG_0 = 'OnGameUpdateDB'
CLOG_0 = 'Field25'
DLOG_0 = 'execute'
ELOG_0 = 'failed' def is_filter(line):
a = line.find(ALOG_0) >= 0
b = line.find(BLOG_0) >= 0
c = line.find(CLOG_0) >= 0
d = line.find(DLOG_0) >= 0
e = line.find(ELOG_0) >= 0 return (a and b and c and d and e)
上一篇:ansible-playbook(nginx例)


下一篇:python基础===新式类与经典类