在制作数据集打标的时候忘记将原图片文件更改文件名就分开了,后不得不根据xml中的标签的数据往回找原图片来修改名字
import xml.dom.minidom
import os
#将文件夹内xml中对应的图片编号存储下来
fileList = os.listdir(r"E:/data_set/output/scratch")
path1="E:/data_set/output/scratch"
path2="E:/data_set/test2"
fileList2 = os.listdir("E:/data_set/test2")
os.chdir(r"E:/data_set/output/scratch")
# def create__file(file_path,msg):
# f=open(file_path,"a")
# f.write(msg+ '\n')
# f.close
for fileName in fileList:
if fileName.endswith('.xml'):
dom = xml.dom.minidom.parse(fileName)
root = dom.documentElement
bb = root.getElementsByTagName('filename')
b = bb[0]
newfileName = os.path.splitext(fileName)
#print(b.firstChild.data)
#create__file("E:/data_set/test2/explode.txt",b.firstChild.data)
for fileName in fileList2:
if fileName == ((str(b.firstChild.data)) + "." + "jpg"):
print(fileName)
kk = newfileName[0]+'.'+'jpg'
print(kk)
#te=os.path.join(path1,fileName)
os.rename(os.path.join(path2,fileName), os.path.join(path1,kk)) #把前者名字改成后者