1、将xml文件批量重命名:
###修改filename
import os
import os.path
from xml.etree.ElementTree import parse
def rename_xml():
path = "D:\\dataset\\garbage-object-detection\\garbage-object-detection\\annotations\\xmls\\"#xml文件目录
for xmlFile in os.listdir(path):
print(xmlFile)
path1 = path+xmlFile
newStr = os.path.join(path, xmlFile)
tree = parse(newStr)
root = tree.getroot()
for obj in root.iter('annotation'):
obj.find('filename').text = xmlFile.split(".")[0]+".jpg"
name1 = obj.find('filename').text
print(name1)
tree.write(path1, xml_declaration=True)
if __name__ == '__main__':
rename_xml()