有两个文件夹,一个文件夹里面的图片是xml,一个文件夹里面是图片,比较两个文件夹里面的前缀是否相同,将不相同的都前缀和改文件名都打印出来,并且删除该文件。
代码:
import os
import xml.etree.ElementTree as ET
def get_image_prefix(file_path):
tree = ET.parse(file_path)
root = tree.getroot()
return root.find('filename').text.split('.')[0]
def compare_and_delete(folder1, folder2):
image_files1 = [f for f in os.listdir(folder1) if f.endswith(".xml")]
image_files2 = [f for f in os.listdir(folder2) if f.endswith('.jpg') or f.endswith('.png')]
for file1 in image_files1:
prefix1 = os.path.splitext(file1)[0]
found = False
for file2 in image_files2:
prefix2 = os.path.splitext(file2)[0]
if prefix1 == prefix2:
found = True
break
if not found:
print(f"不同的前缀: {prefix1}",file1)
os.remove(os.path.join(folder1, file1))
for file2 in image_files2:
prefix2 = os.path.splitext(file2)[0]
found = False
for file1 in image_files1:
prefix1 = os.path.splitext(file1)[0]
if prefix1 == prefix2:
found = True
break
if not found:
print(f"不同的前缀: {prefix2}",file2)
os.remove(os.path.join(folder2, file2))
#文件夹
folder1 = r"C:\Users\29269\Desktop\ww\new\hat\Annotations"
folder2 = r"C:\Users\29269\Desktop\ww\new\hat\JPEGImages"
compare_and_delete(folder1, folder2)
本文暂时没有评论,来添加一个吧(●'◡'●)