|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 yrhhh 于 2020-8-7 20:38 编辑
急求助大佬们一个问题
具体问题就是现在有两个文件夹 A和B (假设A有100张,B有10张,但是A中的100张里面包括B中的10张),现在要从A中删除这10张和B重复的图片
下面的代码可以实现吗?后面部分要不要注释呐 注释掉的部分不注释输出结果和注释掉是一样的。。。。。。后面图片是最终输出了重复图片的名字 但是没有从文件夹中删除 是不是因为这个python2的代码不能在python3实现啊
- #!/usr/bin/env python
- # encoding: UTF-8
- # function : 比较两个文件夹里面的图片,重复的删除
- import glob
- import os
- import numpy as np
- from PIL import Image
- #outDir = os.path.abspath
- #Use the function: os.path.join
- imageDir1 = os.path.abspath('C:/Users/sheryood/Desktop/Compared/Wait_to_delete')#待删除图片的文件夹
- #Define the List of the images
- image1 = []
- #Get the absolute path of the images
- imageList1 = glob.glob(os.path.join(imageDir1, '*.jpg'))
- #Use the function: os.path.basename() Get the name of the images
- for item in imageList1:
- image1.append(os.path.basename(item))
- imageDir2 = os.path.abspath('C:/Users/sheryood/Desktop/Compared')#对比文件夹
- image2 = []
- imageList2 = glob.glob(os.path.join(imageDir2, '*.jpg'))
- for item in imageList2:
- image2.append(os.path.basename(item))
- for item in image1:
- print (item)
- for item in image2:
- print (item)
- for item1 in image1:
- for item2 in image2:
- if item1 == item2:
- """
- dir = imageList1(image1.index( item1 + '.jpg'))
- img = Image.open(dir)
- name = os.path.basename(dir)
- """
- os.remove(imageDir1+'\\'+item1)
- """
- img = Image.open(os.path.join(imageDir2, item1))
- os.remove(os.path.join(imageDir1, item1))
- img.save(os.path.join(outDir, item2))
- img.remove()
- img.remove(os.path.join( item1))
- img.remove(os.path.join(imageDir1, item1))
- """
-
-
复制代码
这是我写的通过效验文件抽样MD5和文件大小的程序,稍微改了改,给你用吧。
代码:
- import hashlib
- from os import walk as os_walk
- from os import sep,remove
- from os.path import getsize,expanduser
- from os.path import join as os_join
- import easygui
- class Delete_Repeat_File:
- def __init__(self,sign=1):
- self.sign = sign
- # 所有遍历到的文件(不包括重复文件)
- self.all_file = {}
-
- # 所有重复文件
- self.all_repeat_file = []
-
- def main(self):
- # 刷新主窗口
- while True:
- file = easygui.diropenbox(f'线程{self.sign}:请选择将要清理的文件夹。')
-
- # 返回-1表示取消操作
- if file == -1:
- return '操作被取消!'
-
- # 集合目录
- _dir = []
- _dir.extend(file)
-
- # 如果所选文件里没有目录
- if not _dir:
- print(f'线程{self.sign}:未指定任何文件目录!')
- continue
- break
-
- # 启动筛选程序
- return self.start(_dir)
-
- # 获取文件MD5
- def get_file_md5(self,file_path):
- with open(file_path, 'rb') as file:
- for each_time in range(3000):
- data = file.read(1024) #将文件分块读取
- if not data:
- break
- self.md5.update(data)
- return self.md5.hexdigest()
-
- # 比对MD5
- def compare_md5(self,file_path):
- self.md5 = hashlib.md5()
- file_md5 = self.get_file_md5(file_path)
- file_size = str(getsize(file_path))
- # 提取相同的文件
- repeat_file = self.all_file.get(file_md5+file_size)
-
- # 如果提取到了相同的文件
- if repeat_file:
- # 刷新主窗口
- self.ui.thread_show(str(self.all_file[file_md5+file_size])+','+str(file_path)+'\n')
- # 将两个文件的路径写入txt
- with open(os_join(expanduser('~'),'Desktop\\重复的文件.txt'),'at',encoding='utf8') as result_file:
- result_file.write(str(self.all_file[file_md5+file_size])+','+str(file_path)+'\n')
- # 记录重复的文件的路径
- self.all_repeat_file.append(file_path)
- # 如果没有提取到相同的文件
- else:
- # 记录非重复文件的路径
- self.all_file[file_md5+file_size] = file_path
- def start(self,dirs):
- # 刷新主窗口
- print(f'线程{self.sign}:正在清理重复文件中。。。')
- # 刷新主窗口
- print(f'线程{self.sign}:开始清理重复文件!')
-
- # 删除旧的txt
- try:remove(os_join(expanduser('~'),'Desktop\\重复的文件.txt'))
- except:pass
-
- # 开始效验文件
- for each_dir in dirs:
- for each_path in os_walk(each_dir):
- for each_file in each_path[2]:
- self.compare_md5(each_path[0]+sep+each_file)
- del self.md5
-
- # 清理内存
- del self.all_file
-
- # 如果找到了重复的文件
- if len(self.all_repeat_file):
- while True:
- # 请求回复
- response = input(f'线程{self.sign}:发现{len(self.all_repeat_file)}个重复的文件,是否删除?【Y\\N】【详情请查看‘桌面\重复的文件.txt】')
- # 删除重复的文件
- if response.casefold() == 'y':
- for each_path in self.all_repeat_file:
- remove(each_path)
- return '重复文件清除完成!'
- # 操作被取消
- elif response.casefold() == 'n':
- return '操作被取消!'
- # 返回了错误的输入
- else:
- self.ui.ai_show(f'线程{self.sign}:输入数据错误!')
- else:
- return '没有发现重复的文件!'
- def main(args,sign=1):
- exec(f'deleter{sign} = Delete_Repeat_File(sign)')
- return eval(f'deleter{sign}.main()')
-
- if __name__ == '__main__':
- deleter = Delete_Repeat_File()
- deleter.main()
复制代码
|
-
|