# -*- coding:utf-8 -*-
from multiprocessing import Pool,Manager
import os
def copyFileTask(name,oldFolderName,newFolderName,queue):
fr = open(oldFolderName+"/"+name)
fw = open(newFolderName+"/"+name,"w")
content = fr.read()
fw.write(content)
fr.close()
fw.close()
queue.put(name)
def main():
#1.获取用户想要copy的文件夹的名字
oldFolderName = input("请输入需要拷贝的用户名:")
#2.创建一个新的文件夹
newFolderName = oldFolderName+"-副本"
os.mkdir(newFolderName)
#3.获取old文件夹里所有文件的名字
fileName = os.listdir(oldFolderName)
#4.用多进程对每个文件进行复制
pool = Pool(3)
queue = Manager().Queue()
for name in fileName:
pool.apply_async(copyFileTask,args=(name,oldFolderName,newFolderName,queue))
num = 0
allNum = len(fileName)
while True:
queue.get()
num +=1
copyRate = num /allNum
print("\r文件已经拷贝了%.2f%%" %(copyRate*100),end="")
if num == allNum:
break
print("\n拷贝完成")
#pool.close()
#pool.join()
if __name__ =="__main__":
main()
|
|