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

yipeiwu_com6年前Python基础

如下所示:

#统计某文件夹下的所有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文件的行数方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python2.7 实现引入自己写的类方法

系统环境:win10 开发环境:JetBrains PyCharm 2017.1.5 x64 Python版本:2.7 假如我们有一个class叫DBUtil,它在A.py里(最好一...

详解python函数的闭包问题(内部函数与外部函数详述)

python函数的闭包问题(内嵌函数) >>> def func1(): ... print ('func1 running...') ... def fu...

pip install urllib2不能安装的解决方法

python35 urllib2 不能用 Could not find a version that satisfies the requirement urllib2 (from...

wxPython实现整点报时

本文实例为大家分享了wxPython整点报时的具体代码,供大家参考,具体内容如下 # C盘要有个wav文件,内含报时音频 import wx import...

Python聊天室实例程序分享

Python聊天室实例程序分享

上一篇 我们学习了简单的Python TCP Socket 编程,通过分别写服务端和客户端的代码了解基本的 Python Socket 编程模型。本文再通过一个例子来加强一下对 Sock...