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)

相关文章

Django项目开发中cookies和session的常用操作分析

本文实例讲述了Django项目开发中cookies和session的常用操作。分享给大家供大家参考,具体如下: COOKIES操作 检查cookies是否存在: request.CO...

Python实现的绘制三维双螺旋线图形功能示例

Python实现的绘制三维双螺旋线图形功能示例

本文实例讲述了Python实现的绘制三维双螺旋线图形功能。分享给大家供大家参考,具体如下: 代码: # -*- coding:utf-8 -*- #! python3 #绘制三维双螺...

Python实现远程调用MetaSploit的方法

本文较为详细的讲述了Python实现远程调用MetaSploit的方法,对Python的学习来说有很好的参考价值。具体实现方法如下: (1)安装Python的msgpack类库,MSF官...

python实现rest请求api示例

该代码参考新浪python api,适用于各个开源api请求 复制代码 代码如下:# -*- coding: utf-8 -*-import collectionsimport gzip...

numpy中实现二维数组按照某列、某行排序的方法

如何根据二维数组中的某一行或者某一列排序?假设data是一个numpy.array类型的二维数组,可以利用numpy中的argsort函数进行实现,代码实例如下: data = da...