网络编程
位置:首页>> 网络编程>> Python编程>> Python实现计算文件夹下.h和.cpp文件的总行数

Python实现计算文件夹下.h和.cpp文件的总行数

作者:junjie  发布时间:2022-09-20 00:54:51 

标签:Python,计算,文件夹,.h,.cpp,文件,总行数

平时自己写了很多代码,但从没好好计算总共写了多少行,面试时被问起来,就傻了。。。闲来无事,写个python程序来统计下


import os

################################################################################
def calcLine(baseDir):
 lineCount = 0

try:
   for fileName in os.listdir(baseDir):

fullPath = baseDir + fileName
     if os.path.isdir(fullPath):
       lineCount += calcLine(fullPath + '\\') #递归读取所有文件

if os.path.splitext(fullPath)[1] in (".h", ".cpp"):
       file = open(fullPath)
       for eachLine in file.readline():
         lineCount += 1
       file.close()

except Exception as e:
   print(e)
 return lineCount

################################################################################
if __name__ == "__main__":
 baseDir = "K:\\C++\\MFC\\BubbleDragon\\"
 lineCount = calcLine(baseDir)
 print(lineCount)
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com