import os
import multiprocessing
def copy_file(queue, file_name, old_folder_name, new_folder_name):
"""完成文件的复制"""
# print("模拟文件复制:从%s--->到%s 复制文件 %s" % (old_folder_name, new_folder_name, file_name))
old_f = open(old_folder_name + "/" + file_name, "rb")
content = old_f.read()
old_f.close()
new_f = open(new_folder_name + "/" + file_name, "wb")
new_f.write(content)
new_f.close()
# 如果拷贝完了文件,那么就向队列中写入一个消息,表示已经完成
queue.put(file_name)
def main():
# 1.获取用户要copy文件夹的名字
old_folder_name = input("请输入要copy的文件夹的名字:")
# 2.创建一个新的文件夹
try:
new_folder_name = old_folder_name + "[复件]"
os.mkdir(new_folder_name)
except:
pass
# 3.获取文件夹的所有的待copy的文件名字, listdir()
file_names = os.listdir(old_folder_name)
# 4.创建进程池
po = multiprocessing.Pool(5)
# 5.创建一个队列
queue = multiprocessing.Manager().Queue()
# 6.向进程池中添加 copy文件的任务
for file_name in file_names:
po.apply_async(copy_file, args=(queue, file_name, old_folder_name, new_folder_name))
po.close()
# po.join()
# 所有的文件个数
all_file_num = len(file_names)
copy_ok_num = 0
while True:
file_name = queue.get()
print("已经完成copy:%s" % file_name)
copy_ok_num += 1
print("拷贝的进度:%0.2f %%" % (copy_ok_num * 100 / all_file_num), end='\r')
if copy_ok_num >= all_file_num:
break
print()
if __name__ == '__main__':
main()
Python_文件夹copy程序代码
最新推荐文章于 2024-05-14 11:32:26 发布