python代码统计
代码统计
修改filename为文件夹or文件地址,然后统计所有python文件代码
import os
import sys
def count_code_lines(filename):
res = os.walk(filename)
count = 0
for path, _, file_list in res:
for file in file_list:
filename = os.path.join(path, file)
if filename.endswith('py'):
with open(filename, 'r', encoding='utf8') as fr:
file_count = 0
for i in fr:
if i.startswith('#') or i.startswith('\n'):
continue
count += 1
file_count += 1
print(f'{filename}有{file_count}行')
print(f'总共有{count}行')
if __name__ == '__main__':
filename = sys.argv[1]
# filename = '目录or文件地址'
# count_code_lines(r'D:\上海python12期视频\python12期视频\项目-atm')
count_code_lines(filename)