import os
from multiprocessing import pool
import multiprocessing
def copyfile(queue, file_name, old_folder_name, new_folder_name):
# 拷贝文件的操作
old_file = open(old_folder_name + "/" + file_name, "rb")
content = old_file.read()
old_file.close()
new_file = open(new_folder_name + "/" + file_name, "wb")
new_file.write(content)
new_file.close()
# 如果拷贝完了文件,就向队列中写入一个消息,表示已经完成
queue.put(file_name)
pass
def main():
# 获取用户要copy的文件夹的名字
old_folder_name = input("请输入要copy的文件夹的名字")
# 创建一个新的文件夹
try:
new_folder_name = old_folder_name + "xin"
os.mkdir(new_folder_name)
except:
pass
# 获取文件夹的所有待copy的文件名字 listdir()
filenames = os.listdir(old_folder_name)
print(filenames)
# 复制源文件夹中的文件,到新文件夹中去
# 创建进程池
po = multiprocessing.Pool(5)
# 创建队列
q = multiprocessing.Manager().Queue()
# 向进程池中添加拷贝文件的任务
for file_name in filenames:
po.apply_async(copyfile, args=(q, file_name, old_folder_name, new_folder_name))
po.close()
# po.join()
all_file_num = len(filenames)
copy_file_num = 0
while True:
file_name = q.get()
print("已经完成copy" + file_name)
copy_file_num += 1
print("\r拷贝的进度为%.2f%%" % (copy_file_num*100 / all_file_num),end="")
print("")
if copy_file_num == all_file_num:
break
if __name__ == "__main__":
main()
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/process/FileCopy.py
请输入要copy的文件夹的名字sss
['__init__.py']
已经完成copy__init__.py
拷贝的进度为100.00%
Process finished with exit code 0
拷贝.png
网友评论