import os
def count_lines_in_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
return len(lines)
def count_lines_in_folder(folder_path):
total_lines = 0
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.py'): # 仅统计.py文件
file_path = os.path.join(root, file)
lines = count_lines_in_file(file_path)
total_lines += lines
return total_lines
def count_lines_recursive(folder_path):
total_lines = 0
for root, dirs, files in os.walk(folder_path):
for dir in dirs:
dir_path = os.path.join(root, dir)
lines = count_lines_in_folder(dir_path)
total_lines += lines
return total_lines
folder_path = './' # 替换为要统计的文件夹路径
total_lines = count_lines_recursive(folder_path)
print(f"Total lines of code in the folder: {total_lines}")
Python统计计算文件夹下所有代码文件的代码行数
于 2023-05-24 17:20:18 首次发布