描述:a文件夹存放1.jpg、2.jpg、3.jpg
b文件夹存放1.txt、3.txt
将a和b前缀同名的文件拷贝到root_path/check下,执行完后新文件夹有1.jpg、3.jpg、1.txt、3.txt
import os
import shutil
root_path = r"D:\desktop\check/"
img_path = r"D:\desktop\fine_cantilever_base\images/" #图像文件夹
txt_path = r"D:\desktop\fine_cantilever_base\xml/" #标签文件夹
def train_test_move(txt_path,root_path,img_path):
if not os.path.exists(root_path):
os.makedirs(root_path)
files = os.listdir(txt_path)
for file in files:
shutil.copy(txt_path+file,root_path)
shutil.copy(img_path+file[:-3]+"jpg",root_path)
train_test_move(txt_path,root_path,img_path)