分支机构的项目管理,基于Django
This commit is contained in:
66
count_lines.py
Normal file
66
count_lines.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
def count_lines_in_file(file_path):
|
||||
"""统计单个文件的行数"""
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
return sum(1 for _ in file)
|
||||
except UnicodeDecodeError:
|
||||
try:
|
||||
with open(file_path, 'r', encoding='gbk') as file:
|
||||
return sum(1 for _ in file)
|
||||
except UnicodeDecodeError:
|
||||
print(f"无法解码文件: {file_path}")
|
||||
return 0
|
||||
except Exception as e:
|
||||
print(f"读取文件 {file_path} 时出错: {e}")
|
||||
return 0
|
||||
|
||||
def count_lines_in_directory(directory, extensions):
|
||||
"""统计目录中特定扩展名文件的总行数"""
|
||||
total_lines = 0
|
||||
file_count = 0
|
||||
|
||||
for root, dirs, files in os.walk(directory):
|
||||
# 跳过__pycache__目录
|
||||
if '__pycache__' in root:
|
||||
continue
|
||||
|
||||
for file in files:
|
||||
if any(file.endswith(ext) for ext in extensions):
|
||||
file_path = os.path.join(root, file)
|
||||
lines = count_lines_in_file(file_path)
|
||||
total_lines += lines
|
||||
file_count += 1
|
||||
print(f"{file_path}: {lines} 行")
|
||||
|
||||
return total_lines, file_count
|
||||
|
||||
def main():
|
||||
project_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# 统计Python文件
|
||||
print("=== Python文件行数统计 ===")
|
||||
py_lines, py_files = count_lines_in_directory(project_dir, ['.py'])
|
||||
print(f"\nPython文件总数: {py_files}")
|
||||
print(f"Python文件总行数: {py_lines}")
|
||||
|
||||
# 统计前端文件 (HTML, CSS, JS)
|
||||
print("\n=== 前端文件行数统计 ===")
|
||||
frontend_extensions = ['.html', '.css', '.js']
|
||||
frontend_lines, frontend_files = count_lines_in_directory(project_dir, frontend_extensions)
|
||||
print(f"\n前端文件总数: {frontend_files}")
|
||||
print(f"前端文件总行数: {frontend_lines}")
|
||||
|
||||
# 总计
|
||||
total_files = py_files + frontend_files
|
||||
total_lines = py_lines + frontend_lines
|
||||
print("\n=== 项目总计 ===")
|
||||
print(f"文件总数: {total_files}")
|
||||
print(f"代码总行数: {total_lines}")
|
||||
print(f"Python文件占比: {py_lines/total_lines*100:.2f}%")
|
||||
print(f"前端文件占比: {frontend_lines/total_lines*100:.2f}%")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user