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)

Posted in 未分类

发表评论