Python遍历目录统计日志总量
知乎专栏:Python学习之路
https://zhuanlan.zhihu.com/pythonlearn
因为笔者目前在网络安全科技公司从事于安全审计,要统计某个系统下高危敏感操作,其中有一个总日志量要统计,拿到客户发来的资料一看,傻眼了,如下:总共44个文件夹
而且每个文件夹下面还有子文件夹,每个子文件夹下面有十几个日志文件,这样子一个一个打开加总数得加到猴年马月去啊。于是乎网上找了一下代码,自己拿来综合改了一下,效果如下:总代码行数:202233,很好就是我要的效果。这个代码就是会遍历指定目录下所有文件夹下最下一层的文件中的代码行数。
附上代码:
首先是 MAC下,py2.7版本的:
#!/usr/bin/python#-*-coding:utf-8 -*-import os,re,io
FILE_PATH = './'
def analyze_code(codefilesource):
''' :param codefilesource: :return: '''
total_line = 0
comment_line = 0
blank_line = 0
with io.open(codefilesource,errors='ignore') as f:
lines = f.readlines()
total_line = len(lines)
line_index = 0
while line_index < total_line:
line = lines[line_index]
if line.startswith("#"):
comment_line += 1
elif re.match("\s*'''",line) is not None:
comment_line += 1
while re.match(".*'''$",line) is None:
line = lines[line_index]
comment_line += 1
line_index += 1
elif line =='\n':
blank_line += 1
line_index += 1
print("在文件:%s中:"%codefilesource)
print("lines:",total_line)
return [total_line,comment_line,blank_line]def list_all_files(rootdir):
_files = []
list = os.listdir(rootdir)
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isdir(path):
_files.extend(list_all_files(path))
if os.path.isfile(path):
_files.append(path)
return _filesdef run():
_fs = list_all_files('./')
total_lines = 0
total_comment_lines = 0
total_blank_lines = 0
for i in _fs:
if True:
line = analyze_code(i)
total_lines,total_comment_lines,total_blank_lines=total_lines+line[0],total_comment_lines+line[1],total_blank_lines+line[2]
print ("====totallines---->:",total_lines)
if __name__ == '__main__':
run()
windows下,py3.6版本的:
#coding:utf-8import os,re
#代码所在目录FILE_PATH = './'
def analyze_code(codefilesource):
''' 打开一个文件,统计其中的代码行数,包括空行和注释 返回含该文件总行数,注释行数,空行数的列表 :param codefilesource: :return: '''
total_line = 0
comment_line = 0
blank_line = 0
with open(codefilesource,encoding='gb18030',errors='ignore') as f:
lines = f.readlines()
total_line = len(lines)
line_index = 0
#遍历每一行
while line_index < total_line:
line = lines[line_index]
#检查是否为注释
if line.startswith("#"):
comment_line += 1
elif re.match("\s*'''",line) is not None:
comment_line += 1
while re.match(".*'''$",line) is None:
line = lines[line_index]
comment_line += 1
line_index += 1
#检查是否为空行
elif line =='\n':
blank_line += 1
line_index += 1
print("在%s中:"%codefilesource)
print("代码行数:",total_line)
return [total_line,comment_line,blank_line]def list_all_files(rootdir):
_files = []
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isdir(path):
_files.extend(list_all_files(path))
if os.path.isfile(path):
_files.append(path)
return _filesdef run():
_fs = list_all_files('./')
total_lines = 0
total_comment_lines = 0
total_blank_lines = 0
for i in _fs:
if True:
line = analyze_code(i)
total_lines,total_comment_lines,total_blank_lines=total_lines+line[0],total_comment_lines+line[1],total_blank_lines+line[2]
print("总代码行数:",total_lines)
if __name__ == '__main__':
run()
Python爱好者社区历史文章大合集:
Python爱好者社区历史文章列表(每周append更新一次)
关注后在公众号内回复“课程”即可获取:
小编的Python入门免费视频课程!!!
【最新免费微课】小编的Python快速上手matplotlib可视化库!!!
崔老师爬虫实战案例免费学习视频。
陈老师数据分析报告制作免费学习视频。
玩转大数据分析!Spark2.X+Python 精华实战课程免费学习视频。