import os
path="D:\\programs\\python\\test"
ex=["common"]
ext=[".html",".htm",".py",".js"]
def calc(file):
f=open(file,mode='r',encoding='utf-8')
c=f.readlines()
f.close()
return len(c)
def list_dir(ph,ex,data):
for f in os.listdir(ph):
f=os.path.join(ph,f)
if os.path.isfile(f) and os.path.splitext(f)[1] in ext:
data.append(f)
if os.path.isdir(f):
if os.path.dirname(f).split('\\')[-1] not in ex:
list_dir(f,ex,data)
data=[]
list_dir(path,ex,data)
s=0
for d in data:
l=calc(d)
print("%s : %d " % (d,l))
s+=l
print("总行数:%d" % s)
非常简单,指定一下目录、排除目录、文件扩展名即可。