import os
import multiprocessing
def copy_filename(q, filename, old_filename, new_filename):
old_f = open(old_filename + '/' + filename, 'rb')
content = old_f.read()
old_f.close()
new_f = open(new_filename + '/' + filename, 'wb')
new_f.write(content)
new_f.close()
# 如果拷贝完了文件,那么就向队列中写入一个消息,表示已经完成
q.put(filename)
def main():
# 1、获取文件夹的名字
old_filename = input('请输入要复制的文件夹名字:')
# 2、创建一个新的文件夹
try:
new_filename = old_filename + '复件'
os.mkdir(new_filename)
except:
pass
# 3、获取文件夹所有的待复制的文件名字
filenames = os.listdir(old_filename)
print(filenames)
# 4、创建进程池
po = multiprocessing.Pool(5)
# 5、创建一个队列
q = multiprocessing.Manager().Queue()
# 6、向进程池中添加 复制文件的任务
for filename in filenames:
po.apply_async(copy_filename, args=(q, filename, old_filename, new_filename))
po.close()
# po.join()
all_filename = len(filenames) #测一下所有的文件个数
copy_okfile = 0
while True:
filename = q.get()
# print('已经完成{}'.format(filename))
copy_okfile += 1
print('
拷贝进度为:{:.2f} %'.format(copy_okfile * 100 / all_filename), end='')
if copy_okfile == all_filename:
break
if __name__ == '__main__':
main()