import os import shutil def make_dir(path): import os dir = os.path.exists(path) if not dir: os.makedirs(path) def get_filename_and_houzhui(full_path): import os path, file_full_name = os.path.split(full_path) file_name, 后缀名 = os.path.splitext(file_full_name) return path,file_name,后缀名 dataset_root_path = '../data/cat_vs_dog' train_path_cat_new = os.path.join(dataset_root_path, 'new/train/cat') train_path_dog_new = os.path.join(dataset_root_path, 'new/train/dog') make_dir(train_path_cat_new) make_dir(train_path_dog_new) image_dir_path = os.path.join(dataset_root_path,'train') image_name_list = os.listdir(image_dir_path) for image_name in image_name_list: image_path = os.path.join(image_dir_path,image_name) path, file_name, 后缀名 = get_filename_and_houzhui(full_path=image_path) print("file_name:", file_name) if('cat' in file_name): shutil.copy(image_path,train_path_cat_new) elif('dog' in file_name): shutil.copy(image_path, train_path_dog_new)