写一个程序统计代码行数,可以统计信息包括:代码行数,注释行数(代码尾部的注释也算),空白行数。并以红,黄,蓝三种颜色分别输出
-
#!/usr/bin/python
-
# coding=utf-8
-
-
import os
-
-
from termcolor import colored
-
-
-
def parse_file(filename):
-
notes_count = 0
-
code_count = 0
-
blank_count = 0
-
with open(filename) as f:
-
for line in f:
-
if line == '\n':
-
blank_count += 1
-
elif line.startswith('#'):
-
notes_count += 1
-
else:
-
code_count += 1
-
notes_count = colored(notes_count, 'red')
-
code_count = colored(code_count, 'yellow')
-
blank_count = colored(blank_count, 'blue')
-
return notes_count,code_count,blank_count
-
-
-
def collect_pyfile(path, depth):
-
file_list = []
-
-
def recurse_dir(_path, _depth):
-
if _depth < 1:
-
return None
-
for d in os.listdir(_path):
-
full_path = os.path.join(_path, d)
-
if os.path.isfile(full_path):
-
if full_path[-3:] == '.py':
-
file_list.append(full_path)
-
else:
-
recurse_dir(full_path, _depth-1)
-
recurse_dir(path, depth)
-
return file_list
-
-
-
if __name__ == '__main__':
-
path = raw_input('please input path: ')
-
depth = int(raw_input('please input depth: '))
-
for pyfile in collect_pyfile(path, depth):
-
print pyfile, '注释行数:%s代码行数:%s空白行数:%s' % parse_file(pyfile)
阅读(2278) | 评论(0) | 转发(0) |