copy_image.py 862 Bytes
Newer Older
“安晓东” committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1、定义一个函数,接受两个参数,第一个参数是原始图片文件,第二个参数是复制到的路径
import shutil
import os

def copy_image(source_file,target_path):
    file_list = os.listdir(target_path)# 显示当前路径下的所有的文件和文件夹
    for image in file_list:
        #如果图像名为B.png 则将B.png复制到F:\\Test\\TestA\\class
        if image == source_file:
            if os.path.exists(os.path.join(target_path,'test')):
                shutil.copy(os.path.join(target_path,image), os.path.join(target_path, 'test'))
                print('复制成功')
            else:
                os.makedirs(os.path.join(target_path,'test'))
                shutil.copy(os.path.join(target_path, image), os.path.join(target_path, 'test'))
copy_image('123.png', r'/workspace/ai_project/lession_9/homework')