python语言实现一个简单的文件拷贝
import os
import time
import multiprocessing
import shutil
def copy_file(list_name,file_name,new_list_name,queue):
with open(list_name + "/" + file_name, 'rb') as f:
content = f.read()
with open(new_list_name + "/" + file_name, "wb") as h:
h.write(content)
queue.put(file_name)
def main():
#1.获取要复制打文件夹名称
list_name = input("请输入文件夹名称:")
#2.新建要复制文件到的新文件夹
new_list_name = "[新]" + list_name
os.mkdir(new_list_name)
#3.遍历要复制的文件夹中的文件
file_name_list = os.listdir(list_name)
#4.遍历列表中的文件打开并复制一份新文件到进程池中
q = multiprocessing.Pool(3)
queue = multiprocessing.Manager().Queue()
for file_name in file_name_list:
q.apply_async(copy_file,(list_name,file_name,new_list_name,queue))
content = 0
while True:
queue.get()
time.sleep(0.01)
content += 1
percent = content*100 / len(file_name_list)
print("\r 当前程序执行进度为%.2f%%"%percent,end="")
if percent == 100:
break
q.close()
q.join()
#print(file_name_list,end = "")
shutil.rmtree(new_list_name) #递归删除文件夹,要看代码执行效果请注释掉当前行代码
if __name__ == '__main__':
main()