import os
from xml.etree.ElementTree import parse, Element
out_dir = 'NewAnnotations/' ##这里是保存的目标文件夹
b = os.listdir('Annotations/')
for i in b:
print(i)
dom = parse('Annotations/'+i)
root = dom.getroot()
print(root)
for obj in root.iter('annotation'):
obj.find('filename').text = "Gray_"+ i.rstrip(".xml")+".jpg"
name1 = obj.find('filename').text
print(name1)
dom.write(out_dir+"Gray_"+i, xml_declaration=True)
##“Gray是我自己定义的,可以改为任意值,i为原来的名字,也可以直接修改成想要的名字”
特此记录一下