"""
统计某个文件夹内的代码行数
1.找出文件夹所有的.py文件
2.所有文件中代码行数累加(统计单个文件中的代码行数)
"""
import os
dirPath = 'E:/offcn/授课班级/9.基地系统0902班'
# 存储所有代码行数
count = 0
count_list = []
def get_lines(file):
# 记录文件代码行数
temp = 0
f = open(file,'r',encoding='utf-8')
for s in f:
temp += 1
return temp
def showFiles(dirpath):
# 文件夹内的所有子文件
files = os.listdir(dirpath)
# 遍历所有子文件
for f in files:
# 子文件的路径为之前文件夹路径+'/'+当前文件名
newpath = dirpath + '/' + f
# 判断新路径是否为文件
if os.path.isfile(newpath):
# 是否为py文件
if newpath.endswith('.py'):
# print(newpath)
c = get_lines(newpath)
# 将数量累加到count中
count_list.append(c)
elif os.path.isdir(newpath):
showFiles(newpath)
showFiles(dirPath)
print(f'代码总行数:{sum(count_list)}')