diff --git a/code_calc.py b/code_calc.py new file mode 100644 index 0000000..a737596 --- /dev/null +++ b/code_calc.py @@ -0,0 +1,30 @@ +import os + +def count_lines_and_comments(directory): + total_code_lines = 0 + total_comment_lines = 0 + + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith('.py'): + file_path = os.path.join(root, file) + code_lines = 0 + comment_lines = 0 + with open(file_path, 'r', encoding='utf-8') as f: + for line in f: + stripped_line = line.strip() + if not stripped_line: # Skip empty lines + continue + if stripped_line.startswith('#'): + comment_lines += 1 + else: + code_lines += 1 + total_code_lines += code_lines + total_comment_lines += comment_lines + print(f"{file_path} - 代码行数: {code_lines}, 注释行数: {comment_lines}") + + print(f"\n'{directory}': 总代码行数: {total_code_lines}, 总注释行数: {total_comment_lines}") + +# 统计指定目录下的Python文件代码行数和注释行数 +directory_path = './PyBot' +count_lines_and_comments(directory_path) \ No newline at end of file