#!/usr/bin/evn python #Author:ELSON_ZENG import os,time import multiprocessing def copy_file(queue, file_list, old_folder_name, new_folder_name ): """完成文件的复制""" #print("===>copy文件:从%s ---> %s 文件名是:%s" %(old_folder_name, new_folder_name,file_list)) f_read = open(os.path.join(old_folder_name,file_list),'rb') f_write = open(os.path.join(new_folder_name,file_list),'wb') while True: content = f_read.read(1024) if content: f_write.write(content) else: break f_write.close() f_read.close() #拷贝完文件,那么就想队列中写入一个消息 queue.put(file_list) def main(): #1、获取用户要copy的文件夹的名字 old_folder_name = input("请输入要copy的文件夹名字:") proccess_num = int(input("请输入要开启的进程数量:")) #2、创建一个新的文件夹 try: new_folder_name = old_folder_name + '_RS' os.mkdir(new_folder_name) except: pass #3、获取文件夹的所有的待copy的文件名字 listdir() file_lists = os.listdir(old_folder_name) #4、创建进程池 p = multiprocessing.Pool(proccess_num) # 5、 创建队列 queue = multiprocessing.Manager().Queue() #5、向进程池中添加copy文件的任务 for file_list in file_lists: p.apply_async(copy_file, args=(queue, file_list, old_folder_name, new_folder_name )) print('-----start-----') p.close() start_time = time.time() #p.join() file_num = len(file_lists) copy_num = 0 while True: file_name = queue.get() copy_num += 1 stop_time = time.time() print(' 拷贝进度为 %.2f %%, 总耗时 %d 秒。' %(copy_num/file_num*100,stop_time - start_time),end = '') if file_num == copy_num: break print(' -----stop------') if __name__ == "__main__": main()