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

yipeiwu_com5年前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)

相关文章

win7下 python3.6 安装opencv 和 opencv-contrib-python解决 cv2.xfeatures2d.SIFT_create() 的问题

1.Anaconda 安装python3.6 conda create -n match python=3.6 Python版本默认安装是 3.6.9 2.安装opencv 执行完毕后,...

Python中turtle库的使用实例

Python中turtle库的使用实例

Turtle库是Python内置的图形化模块,属于标准库之一,位于Python安装目录的lib文件夹下,常用函数有以下几种: 画笔控制函数 penup():抬起画笔; pen...

python调试神器PySnooper的使用

相信很多小伙伴平时写python的时候都是需要调试程序的,出问题了,需要了解函数内部是怎么跑的,而这个时候很多人都会想到在疑惑的地方使用print函数来打印一下参数来调试。虽然用prin...

python pytest进阶之fixture详解

前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytes...

Python的Django REST框架中的序列化及请求和返回

序列化Serialization 1. 设置一个新的环境 在我们开始之前, 我们首先使用virtualenv要创建一个新的虚拟环境,以使我们的配置和我们的其他项目配置彻底分开。 $m...