Python 统计代码行
正在学习 Python, 做了个统计代码行的功能,
参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能,
只是windows下测试了,而且代码文件编码形式是 utf-8的。
如果使用其它编码形式的话,估计修改下代码就行了。
功能特点:
是否统计空行
统计注释
设置忽略文件平
设置统计文件类型
根据不同文件类型,设置注释标签
以下,是代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Created by Aaron <xinlingever@outlook.com> in 2014 2 # This code is for Python 3.4 3 4 import os, sys 5 import traceback 6 7 def strToBool(v): 8 return v.lower() in ("yes", "true", "t", "1") 9 10 exts = ['.js', '.html', '.css', '.h', 'cpp'] 11 commentTags = ['//,/* */','<!-- -->','/* */', '//, /* */', '//, /* */','//, /* */'] 12 commentTypeIndex = ['.js', '.html', '.css', '.cs', '.cpp', '.h'] 13 ignor_folders = ['debug', 'release', 'ipch', 'output', '.svn', '.git', 'durango', 'bld', 'layout'] 14 max_line_limit = 2000 15 16 count_empty_line = False 17 if len(sys.argv) > 1: 18 here = sys.argv[1] 19 else: 20 here = sys.argv[0] 21 22 if(len(sys.argv) > 2): 23 exts = sys.argv[2].split() 24 if(len(sys.argv) > 3): 25 count_empty_line = strToBool(sys.argv[3]) 26 27 28 def read_line_count(fname): 29 count = 0 30 comment_count = 0 31 32 is_in_multi_comment = False 33 multi_line_comment_tag_tailer = '' 34 for line in open(fname, encoding='utf-8').readlines(): 35 if count_empty_line and len(line.strip()) == 0: 36 count += 1 37 else: 38 if len(line.strip()) > 0: 39 count += 1 40 41 # count comment 42 if(is_in_multi_comment): 43 comment_count += 1 44 45 if(line.find(multi_line_comment_tag_tailer) >= 0): 46 is_in_multi_comment = False 47 else: 48 ext = (fname[fname.rindex('.'):]).lower() 49 if ext not in commentTypeIndex: 50 continue 51 52 53 for commentTag in commentTags[commentTypeIndex.index(ext)].split(','): 54 if(len(commentTag.split()) == 1): 55 # single line comment 56 if line.strip().startswith(commentTag): 57 comment_count += 1 58 else: 59 if(line.find(commentTag) >= 0): 60 comment_count += 1 61 62 else: 63 # multi line comment 64 multi_line_comment_tags = commentTag.split() 65 multi_line_comment_tag_header = multi_line_comment_tags[0] 66 multi_line_comment_tag_tailer = multi_line_comment_tags[1] 67 68 if line.find(multi_line_comment_tag_header) >= 0: 69 comment_count += 1 70 is_in_multi_comment = True 71 if line.find(multi_line_comment_tag_tailer) >= 0: 72 is_in_multi_comment = False 73 74 return count,comment_count 75 if __name__ == '__main__': 76 line_count = 0 77 file_count = 0 78 comment_line_count = 0 79 80 subFolderCount = 0; 81 for base, dirs, files in os.walk(here): 82 for file in files: 83 #print(file) 84 # Check the sub directorys 85 if file.find('.') < 0: 86 #print(file) 87 continue 88 89 ext = (file[file.rindex('.'):]).lower() 90 try: 91 if ext in exts: 92 path = os.path.join(base,file) 93 relative_path = path[len(here):].replace(file,'').lower() 94 is_ignore = False 95 for ignorFolder in ignor_folders: 96 if relative_path.find(ignorFolder) >= 0: 97 is_ignore = True 98 break; 99 if is_ignore: 100 continue 101 102 c,c2 = read_line_count(path) 103 if max_line_limit > 0: 104 if c > max_line_limit: 105 continue 106 107 file_count += 1 108 109 print ("\t.%s : %d %d" % (path[len(here):], c, c2)) 110 line_count += c 111 comment_line_count += c2 112 #else: 113 #print(file, "is not in list") 114 except Exception as N: 115 print(traceback.format_exc()) 116 pass 117 print ('File count : %d' % file_count) 118 print ('Line count : %d' % line_count) 119 print ('Comment line count : %d' % comment_line_count) 120 print ('Comment rage is {:.2%}'.format ( comment_line_count / line_count))