比较文件夹
把两个文件夹下的文件名分别放到列表中,对比两个列表不同的地方即可。
''' Description: henggao_note version: v1.0.0 Date: 2022-06-25 16:54:51 LastEditors: henggao LastEditTime: 2022-06-25 17:08:30 ''' # -*- coding: utf-8 -*- import os path1 = 'D:\\视频\\dnf' path2 = 'D:\\视频\\dnf1' def file_name(image_dir,xml_dir): path1_list = [] path2_list = [] for root, dirs, files in os.walk(image_dir): for file in files: path1_list.append(os.path.splitext(file)[0]) for root, dirs, files in os.walk(xml_dir): for file in files: path2_list.append(os.path.splitext(file)[0]) print(len(path1_list)) diff = set(path2_list).difference(set(path1_list)) # 差集,在a中但不在b中的元素 print(diff) for name in diff: print(path1, name + ".jpg") diff2 = set(path1_list).difference(set(path2_list)) # 差集,在b中但不在a中的元素 print(len(diff2)) for name in diff2: print(path2, name + ".txt") if __name__ == '__main__': file_name(path1,path2)
删除两个文件夹下的缺失文件
''' Description: henggao_note version: v1.0.0 Date: 2022-06-25 16:54:51 LastEditors: henggao LastEditTime: 2022-06-25 17:31:35 ''' # -*- coding: utf-8 -*- import os path1 = 'D:\\视频\\dnf\\' path1_type = ".jpg" path2 = 'D:\\视频\\dnf1\\' path2_type = ".txt" def file_name(image_dir, xml_dir): path1_list = [] path2_list = [] for root, dirs, files in os.walk(image_dir): for file in files: path1_list.append(os.path.splitext(file)[0]) for root, dirs, files in os.walk(xml_dir): for file in files: path2_list.append(os.path.splitext(file)[0]) diff = set(path2_list).difference(set(path1_list)) # 差集,在a中但不在b中的元素 print(path1, "文件总数:", len(path1_list), ",相比缺少文件个数:", len(diff), ",缺少文件如下:") # print(diff) for name in diff: print(name + path1_type) # 删除 remove_name = path2+name + path2_type os.remove(remove_name) diff2 = set(path1_list).difference(set(path2_list)) # 差集,在b中但不在a中的元素 print(path2, "文件总数:", len(path2_list), ",缺少文件个数:", len(diff2), ",缺少文件如下:") for name in diff2: print(name + path2_type) # 删除 remove_name = path1+name + path1_type os.remove(remove_name) if __name__ == '__main__': file_name(path1, path2)