吴旭华 发表于 6 天前

对比两个文件夹中的文件名,以确保它们的同等性,并删除那些不匹配的文件

确保两个文件夹中的文件名同等,如果发现某个文件夹中的文件在另一个文件夹中没有对应的文件,则将这些文件删除,以便文件夹中的文件保持同等性。
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('.')

def compare_and_delete(folder1, folder2):
    # image_files1 =
    image_files1 =
    image_files2 =

    for file1 in image_files1:
      prefix1 = os.path.splitext(file1)
      found = False
      for file2 in image_files2:
            prefix2 =os.path.splitext(file2)
            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)
      found = False
      for file1 in image_files1:
            prefix1 = os.path.splitext(file1)
            if prefix1 == prefix2:
                found = True
                break
      if not found:
            print(f"不同的前缀: {prefix2}",file2)
            os.remove(os.path.join(folder2, file2))
#文件夹
folder1 = r"C:\Users\VOCdevkit\txt"
folder2 = r"C:\Users\VOCdevkit\JPEGImages"
compare_and_delete(folder1, folder2)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 对比两个文件夹中的文件名,以确保它们的同等性,并删除那些不匹配的文件