首先,在路径下创建一个以.cpp结尾的c语言源文件
from os.path import isdir,join
from os import listdir
AllLines=[]
NotRepeatedLines=[]
file_num=0
code_num=0
def LinesCount(directory):
global AllLines,NotRepeatedLines,file_num,code_num
for filename in listdir(directory):
temp=join(directory,filename)
if isdir(temp):
LinesCount(temp)
elif temp.endswith('.cpp'):
file_num+=1
with open(temp,'r') as fp:
for line in fp:
if line not in NotRepeatedLines:
NotRepeatedLines.append(line)
code_num+=1
path=r'D:/python'
print(LinesCount('D:/python'))
print('总行数:{0},非重复行数:{1}'.format(code_num,len(NotRepeatedLines)))
print('文件数量:{0}'.format(file_num))
运行后: