多进程实现文件拷贝
import multiprocessing
import os
import shutil
"""
拷贝文件任务:
filename: 文件名
src_dir:源目录
dst_dir:目标目录
"""
def copy_file(file_name, src_dir, dst_dir):
src_file_path = src_dir + "/" + file_name
dst_file_path = dst_dir + "/" + file_name
with open(dst_file_path, "wb") as dst_file:
with open(src_file_path, "rb") as src_file:
while True:
file_data = src_file.read(1024)
if file_data:
dst_file.write(file_data)
else:
break
if __name__ == '__main__':
src_dir = "./test/"
dst_dir = "../test/"
if os.path.exists(dst_dir):
shutil.rmtree(dst_dir)
os.mkdir(dst_dir)
file_list = os.listdir(src_dir)
pool = multiprocessing.Pool(3)
for file_name in file_list:
pool.apply_async(copy_file, (file_name, src_dir, dst_dir))
pool.close()
pool.join()