专业的编程技术博客社区

网站首页 > 博客文章 正文

删除两个文件夹里不相同的的文件(如何批量删除两个文件夹中相同的文件)

baijin 2024-08-23 10:55:53 博客文章 5 ℃ 0 评论

有两个文件夹,一个文件夹里面的图片是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)

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表