Python 进程池复制文件夹案例

    xiaoxiao2021-04-15  282

    import multiporcessing import os import time import random

    def copy_file(queue,file_name,source_folder_name,dest_folder_name): f_read = open(source_folder_name + “/” + file_name, “rb”) f_write = open(dest_folder_name + “/” + file_name, “wb”) while True: time.sleep(random.random()) content = f_read.read(1024) if content: f_write.write(content) else: break f_read.close() f_write.close()

    def main(): source_folder_name = input(“请输入要复制的文件夹的名字:”) dest_folder_name = source_folder_name + “[副本]” try: os.mkdir(dest_folder_name) except: pass file_names = os.listdir(source_folder_name) queue = multiprocessing.Manger().Queue() pool = multiprocessing.Pool(3) for file_name in file_names: pool.apply_async(copy_file, args=(queue,file_name,source_folder_name,dest_folder_name)) pool.close() while True: if not queue.empty(): file_name = queue.get() else: creak

    if name == “main”: main()


    最新回复(0)