import multiprocessing
import os
def copy_file(file_name,source_dir,dest_dir):
# 路径拼接
source_path = source_dir + '/' + file_name
dest_path = dest_dir + '/' + file_name
# 以读的方式打开源文件,以写的方式打开目标文件
with open(source_path,"rb") as source_file:
with open(dest_path,"wb") as dest_file:
# 循环读取源文件到目标路径
while True:
data = source_file.read(1024)
if data:
dest_file.write(data)
else:
break
if __name__ == '__main__':
# 1.定义源文件夹和目标文件
source_dir = "e:/Wr"
dest_dir = "e:/copy"
# 2.创建目标文件夹
try:
os.mkdir(dest_dir)
except:
print("文件夹已存在,无需创建")
# 3.读取源文件夹列表
file_list = os.listdir(source_dir)
# 4.遍历文件列表实现拷贝
for file_name in file_list:
# 创建进程对象
sub_process = multiprocessing.Process(target=copy_file,args=(file_name,source_dir,dest_dir))
# 启动进程
sub_process.start()