Python 统计代码行

时间:2021-01-07 20:07:47

正在学习 Python, 做了个统计代码行的功能,

参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能,

只是windows下测试了,而且代码文件编码形式是 utf-8的。

如果使用其它编码形式的话,估计修改下代码就行了。

功能特点:

是否统计空行

统计注释

设置忽略文件平

设置统计文件类型

根据不同文件类型,设置注释标签

以下,是代码:

 # Created by Aaron <xinlingever@outlook.com> in 2014
# This code is for Python 3.4 import os, sys
import traceback def strToBool(v):
return v.lower() in ("yes", "true", "t", "") exts = ['.js', '.html', '.css', '.h', 'cpp']
commentTags = ['//,/* */','<!-- -->','/* */', '//, /* */', '//, /* */','//, /* */']
commentTypeIndex = ['.js', '.html', '.css', '.cs', '.cpp', '.h']
ignor_folders = ['debug', 'release', 'ipch', 'output', '.svn', '.git', 'durango', 'bld', 'layout']
max_line_limit = 2000 count_empty_line = False
if len(sys.argv) > 1:
here = sys.argv[1]
else:
here = sys.argv[0] if(len(sys.argv) > 2):
exts = sys.argv[2].split()
if(len(sys.argv) > 3):
count_empty_line = strToBool(sys.argv[3]) def read_line_count(fname):
count = 0
comment_count = 0 is_in_multi_comment = False
multi_line_comment_tag_tailer = ''
for line in open(fname, encoding='utf-8').readlines():
if count_empty_line and len(line.strip()) == 0:
count += 1
else:
if len(line.strip()) > 0:
count += 1 # count comment
if(is_in_multi_comment):
comment_count += 1 if(line.find(multi_line_comment_tag_tailer) >= 0):
is_in_multi_comment = False
else:
ext = (fname[fname.rindex('.'):]).lower()
if ext not in commentTypeIndex:
continue for commentTag in commentTags[commentTypeIndex.index(ext)].split(','):
if(len(commentTag.split()) == 1):
# single line comment
if line.strip().startswith(commentTag):
comment_count += 1
else:
if(line.find(commentTag) >= 0):
comment_count += 1 else:
# multi line comment
multi_line_comment_tags = commentTag.split()
multi_line_comment_tag_header = multi_line_comment_tags[0]
multi_line_comment_tag_tailer = multi_line_comment_tags[1] if line.find(multi_line_comment_tag_header) >= 0:
comment_count += 1
is_in_multi_comment = True
if line.find(multi_line_comment_tag_tailer) >= 0:
is_in_multi_comment = False return count,comment_count
if __name__ == '__main__':
line_count = 0
file_count = 0
comment_line_count = 0 subFolderCount = 0;
for base, dirs, files in os.walk(here):
for file in files:
#print(file)
# Check the sub directorys
if file.find('.') < 0:
#print(file)
continue ext = (file[file.rindex('.'):]).lower()
try:
if ext in exts:
path = os.path.join(base,file)
relative_path = path[len(here):].replace(file,'').lower()
is_ignore = False
for ignorFolder in ignor_folders:
if relative_path.find(ignorFolder) >= 0:
is_ignore = True
break;
if is_ignore:
continue c,c2 = read_line_count(path)
if max_line_limit > 0:
if c > max_line_limit:
continue file_count += 1 print ("\t.%s : %d %d" % (path[len(here):], c, c2))
line_count += c
comment_line_count += c2
#else:
#print(file, "is not in list")
except Exception as N:
print(traceback.format_exc())
pass
print ('File count : %d' % file_count)
print ('Line count : %d' % line_count)
print ('Comment line count : %d' % comment_line_count)
print ('Comment rage is {:.2%}'.format ( comment_line_count / line_count))