Python统计项目代码的行数

功能

统计Python、Java、C++等语言的项目内代码文件行数并输出。
使用前需安装prettytable用于美化输出
pip install prettytable

代码

# coding=utf-8
import os
import sys
import prettytable as pt
import codecs

# 指定想要统计的文件类型
CPP_SUFFIX_SET = {
     'h', 'hpp', 'hxx', 'c', 'cpp', 'cc', 'cxx'}
PYTHON_SUFFIX_SET = {
     'py'}
JAVA_SUFFIX_SET = {
     'java'}

def list_files(project_path):
    # 遍历文件, 递归遍历文件夹中的所有
    filelists = []
    for parent, dirnames, filenames in os.walk(project_path):
        for filename in filenames:
            filelists.append(os.path.join(parent, filename))
    return filelists

def count_line(filename):
    # 统计一个文件的行数
    count = 0
    with codecs.open(filename, 'r', 'utf-8') as f:
        for line in f.readlines():
            line = line.strip()  # 删除行首的空格
            if line != '' and line != '\n':   # 过滤掉空行
                if line[0] != '#' and line[0] != '/':  # 过滤掉注释行
                    count += 1
    return count

def statistics(filelist):
    cpp_lines = 0
    python_lines = 0
    java_lines = 0
    for filename in filelist:
        ext = filename.split('.')[-1]
        if ext in CPP_SUFFIX_SET:
            cpp_lines += count_line(filename)
        elif ext in PYTHON_SUFFIX_SET:
            print(filename)
            python_lines += count_line(filename)
        elif ext in JAVA_SUFFIX_SET:
            java_lines += count_line(filename)
    total_lines = cpp_lines + python_lines + java_lines
    return cpp_lines, python_lines, java_lines, total_lines

def print_result(cpp_lines, python_lines, java_lines, total_lines):
    tb = pt.PrettyTable()
    tb.field_names = ['CPP', 'PYTHON', 'JAVA', 'TOTAL']
    tb.add_row([cpp_lines, python_lines, java_lines, total_lines])
    print(tb)


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("请指定项目路径!")
    else:
        project_path = sys.argv[1]
        filelists = list_files(project_path)
        print_result(*statistics(filelists))

运行:python statistics.py D:\work\project
输出结果如下:
在这里插入图片描述

参考资料:
[1] Python 统计代码的行数,Python脚本 统计代码
[2] 使用PYTHON统计项目代码行数

你可能感兴趣的:(python,python)