首页 > 编程语言 > 详细

python统计c++源程序文件中不重复代码行数

时间:2020-03-09 22:36:26      阅读:80      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-

from os.path import isdir, join
from os import listdir

AllLines = []           # 保存所有代码行
NotRepeatedLines = []   # 保存非重复的代码行

file_num = 0      # 文件数量
code_num = 0      # 代码总行数

def LinesCount(directory):
    global AllLines
    global NotRepeatedLines
    global file_num
    global code_num
    
    for filename in listdir(directory):
        temp = join(directory, filename)
        if isdir(temp):                # 递归遍历子文件夹
            LinesCount(temp)
        elif temp.endswith(.cpp):    #  只考虑.cpp文件
            file_num += 1
            with open(temp, r) as fp:
                while True:
                    line = fp.readline()
                    if not line:
                        break
                    if line not in NotRepeatedLines:
                        NotRepeatedLines.append(line)  # 记录非重复行
                    code_num += 1                      # 记录所有代码行
    
    return (code_num, len(NotRepeatedLines))

path = G:/Dev-Cpp/代码大全/Offer
print(代码总数量: {0[0]}, 非重复代码行数: {0[1]}.format(LinesCount(path)) )
print(文件数量: {0}.format(file_num))

 

python统计c++源程序文件中不重复代码行数

原文:https://www.cnblogs.com/douzujun/p/12452027.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!