读取某个目录下各个文件的总行数​

本文介绍了一种批量统计指定目录下所有文件行数的方法,并按后缀名进行分类汇总。代码示例清晰易懂,适合Python初学者和中级开发者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取某个目录下所有文件的行数以后缀名分类读取总行数


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()




转载于:https://my.oschina.net/guanshanyue1990/blog/226113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值