shutil.copyfile复制文件到另一个文件夹中

shutil.copyfile(file1,file2)

file1为需要复制的源文件的文件路径,file2为目标文件的文件路径+文件名.

如下:将c盘中A文件夹下的0.png复制到D盘中B文件夹下并重命名为1.png.

src_file = 'C:\\A\\0.png'
dst_file = 'D:\\B\\1.png'
shutil.copyfile(src_file,dst_file)

如果不想修改名字可以使用shutil.copy(file,folder) .

如下:将src_file中的所有文件复制到dst_file中.

dst_file = './test_copy2'
src_file = './test_copy1'
current_list = glob.glob(os.path.join(src_file,'*'))

for x in current_list:
    shutil.copy(x,dst_file)

下面是我的笔记,请忽略.

import os
import glob
import shutil

def imsave_input_new(file,save_path,save_name):
    original_path = os.getcwd()+'/image/input_new'
    if not os.path.isdir(original_path):
        os.makedirs(original_path)
    path = os.path.join(original_path,save_path)
    if not os.path.isdir(path):
        os.makedirs(path)
    dst_file = os.path.join(path,'{}.png'.format(save_name))
    shutil.copyfile(file,dst_file)

#复制input图像到input_new文件中
input_dir = glob.glob(os.path.join(os.getcwd()+"/image/input/","*"))
input_dir.sort(key=lambda x:int(x.split('/')[-1]))

for i in input_dir:
    input_sub = glob.glob(os.path.join(i,'*'))
    input_sub.sort(key=lambda x:int(x.split('/')[-1]))
    path_name = i.split('/')[-1]
    for j in input_sub:
        depart_list = glob.glob(os.path.join(j,'*'))
        depart_list.sort(key=lambda x:int(x.split('/')[-1].split('.')[0]))
        save_name = j.split('/')[-1]
        imsave_input_new(depart_list[0],path_name,save_name)

猜你喜欢

转载自blog.csdn.net/Li_haiyu/article/details/89207435