欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

对Python 多线程统计所有csv文件的行数方法详解

程序员文章站 2023-11-01 13:37:34
如下所示: #统计某文件夹下的所有csv文件的行数(多线程) import threading import csv import os class...

如下所示:

#统计某文件夹下的所有csv文件的行数(多线程)
import threading
import csv
import os
 
class mythreadline(threading.thread): #用于统计csv文件的行数的线程类
 def __init__(self,path):
  threading.thread.__init__(self) #父类初始化
  self.path=path #路径
  self.line=-1 #统计行数
 def run(self):
  reader = csv.reader(open(self.path, "r")) # 读取csv文件
  lines=0
  for item in reader: # 读取每一行
   lines+=1
  self.line=lines #保存行数
  print(self.getname(),self.line)
 
 
path="c:\\users\\aa\\csv" #所有csv文件所在的文件夹
filelist=os.listdir(path) #存储了所有的csv文件名
threadlist=[] #线程列表
for filename in filelist:
 newpath=path+"\\"+filename #代表绝对路径
 mythd=mythreadline( newpath) #创建线程类对象
 mythd.start() #线程开始干活
 threadlist.append(mythd) #增加线程到线程列表
for mythd in threadlist: #遍历每一个线程
 mythd.join() #等待所有线程干完活,再继续执行以下代码
linelist=[] #csv文件行数列表
for mythd in threadlist:
 linelist.append(mythd.line)
print(linelist)
 

以上这篇对python 多线程统计所有csv文件的行数方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。