- A+
所属分类:Seay信息安全博客
显示不全请点击全屏阅读
要统计一些大文件的函数,文件太大太多,只有写脚本来跑了,统计速度很快。windows类似linux wc统计文件行数python脚本。
在windows 的环境变量里设置下这个文件所在目录,就可以直接使用命令了。
# -*- coding: GBK -*
# Author: Seay
# Blog :www.cnseay.com
import os,sys
def func_countfileline(filepath):
num =0
thefile=open(filepath, 'rb')
while True:
buffer = thefile.read(104857600)
if not buffer:
break
num += buffer.count('\n')
thefile.close()
print '\t-- '+str(num+1)
return num+1
def func_walks(path):
line_count=0
file_count=0
for root, dirs, files in os.walk(path):
for f in files:
f = os.path.join(root, f)
print f,
file_count+=1
line_count+=func_countfileline(f)
return '\n\n文件数:'+str(file_count)+'\n总行数:'+str(line_count)
if __name__ == '__main__':
if len(sys.argv)>=3 and (sys.argv[1]=='-f' or sys.argv[1]=='-d'):
if sys.argv[1]=='-f' and os.path.isfile(sys.argv[2]):
print sys.argv[2],
print '\n\n总行数: ' + str(func_countfileline(sys.argv[2]))
elif sys.argv[1]=='-d' and os.path.exists(sys.argv[2]):
print func_walks(sys.argv[2])
else:
print '-- 文件(夹)'+sys.argv[2]+'不存在'
elif len(sys.argv)==1:
print func_walks(os.getcwd())
else:
print '-- 参数说明 :'
print ' 1. '+sys.argv[0]+ ' -f' +' filename \t统计指定文件行数'
print ' 2. '+sys.argv[0]+ ' -d' +' directory \t统计指定目录(包括子目录)下的文件总行数'
print ' 3. '+sys.argv[0]+ ' \t统计当前目录(包括子目录)下的文件总行数'
Tags:
如果您喜欢我的博客,欢迎点击图片定订阅到邮箱
也可以点击链接【订阅到鲜果】
如果我的想法或工具帮助到了你,也可微信扫下方二维码打赏本人一杯咖啡



