您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:18:20 python 字数 1771 阅读 351 来源 www.jb51.cc/python

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

概述

平时自己写了很多代码,但从没好好计算总共写了多少行,面试时被问起来,就傻了。。。闲来无事,写个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)

总结

以上是编程之家为你收集整理的Python实现计算文件夹下.h和.cpp文件的总行数全部内容,希望文章能够帮你解决Python实现计算文件夹下.h和.cpp文件的总行数所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶