省略遍历目录步骤。(可通过遍历目录后循环实现,期间不要重置计数变量)
import os
import string
import re
os.chdir('C:/workspace')
fh=open('test_test.py')
read_fh=fh.readlines()
fh.close()
number_code=0
number_empty=0
number_note=0
pattern='.*#' #正则匹配模式
for x in read_fh:
if '#' in x: #计算注释数目
if re.findall(pattern,x)[0][:-1].isspace() or re.findall(pattern,x)[0][:-1]=='':
number_note+=1
else:
number_code+=1
elif x.isspace():
number_empty+=1
else:
number_code+=1
print 'code number is %d'%(number_code+number_empty+number_note)
print 'empty number is %d'%number_empty
print 'note number is %d'%number_note
本文介绍了一个简单的Python脚本,用于统计指定文件夹内Python文件的代码行数、空行数及注释行数。通过正则表达式匹配和条件判断实现了基本的统计功能。

被折叠的 条评论
为什么被折叠?



