【复试 python程序设计 第2版 董付国】python统计c++源程序文件中不重复代码行数...

本文介绍了一个用于统计C++代码行数及非重复代码行数的Python脚本,该脚本能够递归地遍历指定目录及其子目录下的所有C++文件,并返回总的代码行数和非重复代码行数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- 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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值