import os
# 指定两个文件夹路径
jpg_folder_path = r'E:\压缩包合集\VOC2028\VOC2028\JPEGImages'
xml_folder_path = r'E:\压缩包合集\VOC2028\VOC2028\Annotations'
# 获取jpg文件夹中的文件列表
jpg_files = [os.path.splitext(file)[0] for file in os.listdir(jpg_folder_path) if file.lower().endswith('.jpg')]
# 获取xml文件夹中的文件列表
xml_files = [os.path.splitext(file)[0] for file in os.listdir(xml_folder_path) if file.lower().endswith('.xml')]
# 找出在jpg文件夹中存在但在xml文件夹中不存在的文件
files_to_remove = [os.path.join(xml_folder_path, file + '.xml') for file in xml_files if file not in jpg_files]
# 删除在xml文件夹中多余的文件
for file_path in files_to_remove:
os.remove(file_path)
print(f'File "{os.path.basename(file_path)}" deleted from xml folder')