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

yipeiwu_com6年前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 集合 并集、交集 Series list set 转换的实例

set转成list方法如下: list转成set方法如下: s = set('12342212')       &n...

python使用pygame框架实现推箱子游戏

python使用pygame框架实现推箱子游戏

本代码来源于 《Python和Pygame游戏开发指南》中的 Star Pusher 游戏,供大家参考,具体内容如下 # Star Pusher (a Sokoban clone)...

Python PIL图片添加字体的例子

Python PIL图片添加字体的例子

效果 左边原图,右面添加字体后保存的图。 代码 # -*- coding: utf-8 -*- import PIL.Image as Image import PIL.Image...

Python类的继承用法示例

本文实例讲述了Python类的继承用法。分享给大家供大家参考,具体如下: python —类的继承 root@kali:~/python/mod# vi class2.py root...

Python字符串匹配算法KMP实例

本文实例讲述了Python字符串匹配算法KMP。分享给大家供大家参考。具体如下: #!/usr/bin/env python #encoding:utf8 def next(patt...