统计目录下程序源程序的行数之和-Python

一个比较实用的功能,统计一个工程下面,某种代码的总行数。

以Unity开发的C#为例,整个目录下面只有三种文件:文件夹、cs文件、meta文件。

 

 

我们的目标是检索该目录下以及所有的子文件夹内的cs文件,并统计cs文件的内容的行数之和。

**以下代码都需要 import os

1. 首先找到所有的cs文件,只需要递归查找即可

def find_all_cs_files(path):
    if os.path.isdir(path):
        for sub_dir in os.listdir(path):
            find_all_cs_files(os.path.join(path, sub_dir))
    elif path[-2:] == "cs":
        print(path)
if __name__ == '__main__':
    find_all_cs_files("D:/ExecuSimu/ExecuDS10/Assets/Scripts")

打印结果如下:

D:/ExecuSimu/ExecuDS10/Assets/Scripts\AdjListCreate.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\AdjMatrixCreate.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\AvgData.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\BackGround.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\button.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\ChangeImage.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\ChangeImage2.cs
D:/ExecuSimu/ExecuDS10/Assets/Scripts\City.cs

......

 

2. 然后实现统计一个cs文件代码行数的功能,其实就是读取该文件,并逐行读取,每读取一行,就将结果加1,读取完毕之后,最后返回结果

def count_file_line(path):
    print("当前文件:", path)
    count = 1
    with open(path, 'r', encoding="utf_8_sig") as f:
        line = f.readline()
        while line:
            line = f.readline()
            count += 1
    return count
if __name__ == '__main__':
    line_count = count_file_line("D:/ExecuSimu/ExecuDS10/Assets/Scripts\Proof\ClueManager.cs")
    print(line_count)

通过记事本打开该文件,确认目标代码文件是92行,而该段程序打印结果也是92行,代码正确。

 

3. 然后修改递归查找的代码,定义一个全局变量,然后递归结构中,每找到一个cs文件,便统计该文件内容的行数,加到全局变量中,最后返回结果。

import os


def count_file_line(path):
    print("当前文件:", path)
    count = 1
    with open(path, 'r', encoding="utf_8_sig") as f:
        line = f.readline()
        while line:
            line = f.readline()
            count += 1
    return count


def find_all_cs_files(path):
    if os.path.isdir(path):
        for sub_dir in os.listdir(path):
            find_all_cs_files(os.path.join(path, sub_dir))
    elif path[-2:] == "cs":
        print(path)


sum_line_count = 0


def count_dir_files_code_lines(path):
    global sum_line_count
    if os.path.isdir(path):
        for sub_dir in os.listdir(path):
            count_dir_files_code_lines(os.path.join(path, sub_dir))
    # elif path[-len(category):] == category:
    elif path[-2:] == "cs":
        sum_line_count += count_file_line(path)
        print("当前计数:", sum_line_count)
    return sum_line_count


if __name__ == '__main__':
    line_count = count_dir_files_code_lines("D:/ExecuSimu/ExecuDS10/Assets/Scripts")
    print(line_count)

将代码稍作修改,可以统计其他类型的文件代码行数,以统计文件夹下所有的java代码行数为例,在以上代码的基础上,做以下改动即可:

def count_dir_files_code_lines(path, category):
    global sum_line_count
    if os.path.isdir(path):
        for sub_dir in os.listdir(path):
            count_dir_files_code_lines(os.path.join(path, sub_dir), category)
    elif path[-len(category):] == category:
        # elif path[-2:] == "cs":
        sum_line_count += count_file_line(path)
        print("当前计数:", sum_line_count)
    return sum_line_count


if __name__ == '__main__':
    line_count = count_dir_files_code_lines("D:/javaworkspace/asoul-applications/src", 'java')
    print(line_count)

如果需要统计所有文件的行数,那么在函数 count_dir_files_code_lines(path, category) 中的 elif 改为 else即可

 

posted @ 2021-10-08 19:14  倦鸟已归时  阅读(132)  评论(0编辑  收藏  举报