import os
# 图片文件夹路径
image_folder_path = r"D:\Desktop\新建文件夹\img"
# txt文件夹路径
txt_folder_path = r"D:\Desktop\新建文件夹\xml"
# 获取所有图片文件的文件名,不包括扩展名
image_files = set(os.path.splitext(filename)[0] for filename in os.listdir(image_folder_path) if filename.endswith('.jpg') or filename.endswith('.png'))
# 遍历文本文件夹,删除没有对应图片文件的文本文件
for txt_filename in os.listdir(txt_folder_path):
if txt_filename.endswith('.xml'):
txt_name = os.path.splitext(txt_filename)[0]
if txt_name not in image_files:
txt_path = os.path.join(txt_folder_path, txt_filename)
os.remove(txt_path)
print(f"Deleted {txt_filename} because the corresponding image file was not found.")
04-27
3万+