linux 下python多线程递归复制文件夹

本文是利用python 复制文件夹 刚开始写了一个普通的递归复制文件夹    然后想了想 觉得对io频繁的程序 threading 线程还比较友好  就写了个多线程版本的  最恶心人的地方就是路径  其他都还好吧 

import os
import threading
import multiprocessing


length_of_folder = 0

def copyfile(Path):
    if os.path.isdir(Path):
        print("-----------%s" % ("Testfortherading_" + '/' + Path[length_of_folder:]))
        os.makedirs("Testforthreading_" + '/' + Path[length_of_folder:])
    filenames = os.listdir(Path)
    for filename in filenames:
        if os.path.isdir(Path + '/' + filename):
            #ps = "Testforthreading_" +"/" + Path[length_of_folder:]
            #print("%s" % (ps + '/' + filename))
            #os.mkdir(ps + '/' + filename)
            temp = Path + '/' + filename
            t = threading.Thread(target=copyfile , args=(temp,))
            t.start()

        else:
            f = open(Path + '/' + filename , 'rb')
            content = f.read()
            F = open('Testforthreading_' + '/' + Path[length_of_folder:]+ '/' + filename , 'wb')
            F.write(content)
            f.close()
            F.close()

def main():
    """"""

    foldername = input("Please input the folder you want to copy:")
    length_of_folder = len(foldername)
    if os.path.isdir("Testforthreading_"):
        os.removedirs("Testforthreading_")
    os.mkdir("Testforthreading_")
    copyfile(foldername)
    #p = multiprocessing.Pool(10)
    #que = multiprocessing.Manager().Queue()



if __name__ == "__main__":
    main()

 

你可能感兴趣的:(Python,python,多线程,递归)