删掉两个文件中不对应的xml和jpg

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')

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值