统计指定后缀文件的代码行数
通过递归的方式获取文件夹内指定后缀类型所有文件的行数,用于统计代码行数
import os
def getFileNames(path, list_name, postfx):
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
getFileNames(file_path, list_name, postfx)
else:
suffix = os.path.splitext(file_path)
if len(suffix)==2:
for item in postfx:
if item==suffix[1]:
list_name.append(file_path)
if __name__=="__main__":
path=R'D:\VisualStudio2017\src'
list_name=[]
postfix = {'.h', '.cpp'}
getFileNames(path, list_name, postfix)
lineNum=0
for file in list_name:
print(file)
with open(file, 'r', encoding='gb2312') as f:
for line in f:
lineNum = lineNum + 1
print(lineNum)
print(len(list_name))