脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 对Python 多线程统计所有csv文件的行数方法详解

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

2021-05-27 00:18houyanhua1 Python

今天小编就为大家分享一篇对Python 多线程统计所有csv文件的行数方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#统计某文件夹下的所有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文件的行数方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/houyanhua1/article/details/78249189

延伸 · 阅读

精彩推荐