對Python 多線程統(tǒng)計所有csv文件的行數方法詳解
更新時間:2019年02月12日 09:35:27 作者:houyanhua1
今天小編就為大家分享一篇對Python 多線程統(tǒng)計所有csv文件的行數方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
#統(tǒng)計某文件夾下的所有csv文件的行數(多線程) import threading import csv import os class MyThreadLine(threading.Thread): #用于統(tǒng)計csv文件的行數的線程類 def __init__(self,path): threading.Thread.__init__(self) #父類初始化 self.path=path #路徑 self.line=-1 #統(tǒng)計行數 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) #創(chuàng)建線程類對象 mythd.start() #線程開始干活 threadlist.append(mythd) #增加線程到線程列表 for mythd in threadlist: #遍歷每一個線程 mythd.join() #等待所有線程干完活,再繼續(xù)執(zhí)行以下代碼 linelist=[] #csv文件行數列表 for mythd in threadlist: linelist.append(mythd.line) print(linelist)
以上這篇對Python 多線程統(tǒng)計所有csv文件的行數方法詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python2.x利用commands模塊執(zhí)行Linux shell命令
這篇文章主要介紹了Python2.x利用commands模塊執(zhí)行Linux shell命令 的相關資料,需要的朋友可以參考下2016-03-03PyCharm Python Console中文輸出亂碼問題及解決
這篇文章主要介紹了PyCharm Python Console中文輸出亂碼問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07