import os
import xml.etree.ElementTree as ET
def find_xmls_with_class(xml_dir, class_name):
xml_files_with_class = []
file_names = [name.split('.')[0] for name in os.listdir(xml_dir)]
for file_name in file_names:
xml_path = os.path.join(xml_dir, file_name+'.xml')
tree = ET.parse(xml_path)
root = tree.getroot()
for obj in root.findall('object'):
name = obj.find('name').text
if name == class_name:
xml_files_with_class.append(file_name+'.xml')
break
return xml_files_with_class
dir_path = './'
class_name = 'airplane'
xml_dir = os.path.join(dir_path, 'Annotations')
xml_files_with_class = find_xmls_with_class(xml_dir, class_name)
for xml_file in xml_files_with_class:
print(xml_file)