**
实现从‘延时摄影’文件夹中复制文件到‘test’文件夹
**
#!/usr/bin/env python3
import os
import multiprocessing
def copy_file(file_name, source_dir ,dest_dir): #复制操作
#拼接文件路径和目标路径:
source_path = source_dir+'/'+file_name
dest_dir = dest_dir+'/'+file_name
#打开源文件和目标文件:
with open(source_path,'rb') as source_file:
with open(dest_dir,'wb') as dest_file:
while True:
data = source_file.read(1024)
if data:
dest_file.write(data)
else:
break
if __name__ =='__main__':
source_dir = '/Users/marscolono/Desktop/延时摄影'
dest_dir = '/Users/marscolono/Desktop/test'
try:
os.mkdir(dest_dir)
except:
print('target file had exited')
file_list = os.listdir(source_dir)
for file_name in file_list:
# copy_file(file_name, source_dir ,dest_dir)
# 多进程:
sub_process = multiprocessing.Process(target=copy_file, args = (file_name, source_dir ,dest_dir))
sub_process.start()