BZ易风

导航

 
 1 import os
 2 import multiprocessing
 3 
 4 
 5 def copy_file(q,file_name,new_folder_name,old_folder_name):
 6     with open(old_folder_name + "/" + file_name,"rb") as f1, open(new_folder_name + "/" + file_name,"wb") as f2:
 7         content = f1.read()
 8         f2.write(content)
 9     q.put(file_name)
10 
11 
12 
13 def main():
14     # 1.获取文件夹名字
15     old_folder_name = input("请输入想要copy的文件夹的名字:")
16     # 2.创建一个新文件夹
17     try:
18         new_folder_name = old_folder_name + "[复件]"
19         os.mkdir(new_folder_name)
20     except:
21         # 如果创建失败直接pass
22         pass
23     # 3.获取文件夹里文件的名字
24     file_names = os.listdir(old_folder_name)
25     # 4.创建进程池
26     po = multiprocessing.Pool(5)
27     q = multiprocessing.Manager().Queue()
28     for file_name in file_names:
29         # 5.复制文件到新文件夹里去
30         po.apply_async(copy_file,(q,file_name,new_folder_name,old_folder_name))
31 
32     po.close()
33 
34     # 计算数据
35     copy_ok_num = 0
36     while True:
37         new_file_name = q.get()
38         # 记录复制完成的数量
39         copy_ok_num+=1
40         print("\r拷贝的进度为%.2f %%" % (copy_ok_num * 100 / len(file_names)),end="")
41         if copy_ok_num >= len(file_names):
42             break
43 
44 
45 if __name__ == '__main__':
46     main()
请输入想要copy的文件夹的名字:test
拷贝的进度为100.00 %

  

posted on 2019-08-21 16:37  BZ易风  阅读(237)  评论(0编辑  收藏  举报