对目录下所有的代码文件的行数进行计数
给出工程路径、指定代码类型,计算总共有多少行代码。
以下代码的原理是,递归搜索文件夹下的源码文件,然后统计该文件有多少行,然后累加。
# -*- coding: utf-8 -*- # @Author : ZhaoKe # @Time : 2021-10-08 18:08 import os sum_line_count = 0 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 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("E:/ExecuSimu/ExecuDS10/Assets/Scripts", 'cs') print(line_count)
以上代码以C#文件为例,文件后缀为"cs",能够给出正确结果。
不过上面这个代码写的并不好,首先既然已经声明全局变量了,把全局变量作为函数返回值,属实过于冗余,而且可以直接把每一行直接加到全局变量上,另外,其实 len(f.readlines()) 就是某文件的行数!
另外,如此做,也把空白行统计进去了,不够严谨。
# -*- coding: utf-8 -*- # @Author : ZhaoKe # @Time : 2021-10-08 18:08 import os sum_line_count = 0 sum_line_count_without_blank = 0 def count_file_line(path): global sum_line_count global sum_line_count_without_blank with open(path, 'r', encoding="utf_8_sig") as f: # ------------ # print(path) # print("line count: ", len(f.readlines())) # count += len(f.readlines()) # ------------ line = f.readline() while line: line = f.readline() if line is not "": sum_line_count_without_blank += 1 sum_line_count += 1 def count_dir_files_code_lines(path, category): 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: count_file_line(path) if __name__ == '__main__': directory_path = "D:/javaworkspace/designPatterns/src" print("target path", directory_path) count_dir_files_code_lines(directory_path, 'java') print("sum of line count: ", sum_line_count) print("sum of line count without blank line: ", sum_line_count_without_blank)
如此就优化了,同时统计文件行数和非空白的行数。