Files
fzjg_local/fzjgact/统计python行数.py

29 lines
974 B
Python
Raw Permalink Normal View History

from pathlib import Path
import datetime
def count_lines_of_code(directory):
total_lines = 0
directory_path = Path(directory)
for file_path in directory_path.rglob('*.py'):
with file_path.open('r', encoding='utf-8') as f:
lines = f.readlines()
total_lines += len(lines)
return total_lines
if __name__ == '__main__':
directory = input('请输入要统计的Python代码所在的目录')
total_lines = count_lines_of_code(directory)
# 获取当前日期时间
now = datetime.datetime.now()
date_time_str = now.strftime('%Y-%m-%d %H:%M:%S')
# 将日期时间和总行数格式化为字符串
result_str = f'\r\n日期时间:{date_time_str}\n统计的总行数:{total_lines}'
# 将结果写入result.txt文件
with open('result.txt', 'a', encoding='utf-8') as result_file:
result_file.write(result_str)
print('统计结果已写入result.txt文件。')