读取某个目录下所有文件的行数以后缀名分类读取总行数
import os.path
from os.path import getsize,join
#import string
def fun_lines(files):
infile=open(files,'r')
countline=0
for line in infile:
countline +=1
return countline
infile.close()
def fun_rowcount():
d_dir=raw_input('enter a exists directory: ')
type_file=[]
type_file1=[]
for dir_name, dirs, files in os.walk(d_dir):
for line in range(0,len(files)):
path_file=dir_name+'/'+files[line]
type1=os.path.splitext(path_file)[1]
type_file.append(type1)
type_file1.append(path_file)
type_file=list(set(type_file))
countline=0
for i in range(0,len(type_file)):
for j in range(0,len(type_file1)):
if type_file[i] == os.path.splitext(type_file1[j])[1] :
countline+=fun_lines(type_file1[j])
print 'the file type is: ',type_file[i],' and rowlines is: ',countline
fun_rowcount()