PythonHackingBook1 icon indicating copy to clipboard operation
PythonHackingBook1 copied to clipboard

操作多线程

Open hong-xiaobo opened this issue 3 years ago • 0 comments

import time
import os
class Mythread(Thread):
    def __init__(self,path):
        super().__init__()
        self.path=path

    def run(self):
        path=self.path
        files = []
        dirs = []
        for (dirpath, dirnames, filenames) in os.walk(path):
            # files += filenames
            # dirs += dirnames
            # print(files)
            # print(dirs)
            for fn in filenames:
                filepath=os.path.join(dirpath,fn)
                with open(filepath,encoding='utf8')as f:
                    text=f.read()
                    print(text)

filepath=[r'C:\Users\10396\Desktop\新建文件夹',r'C:\Users\10396\Desktop\新建文件夹 (2)']

t=[]
for i in range(2):
    t1=Mythread(filepath[i])
    #print(f'我是主线程{i}')
    t1.start()
    t.append(t1)

for i in t:
    i.join()

hong-xiaobo avatar May 21 '21 11:05 hong-xiaobo