春天来了,花园里开满了花,有梅花,有迎春花,有山茶花,有丁香花,真是太好看了。
前天不小心把硬盘格式化了,丢了好多照片,后来用Recuva这款软件成功把文件恢复过来,可是恢复的文件中有好多重复的文件和无法打开的图片,所以写了两个python的小程序用来解决这个问题
删除相同文件:
#coding=utf-8 import os import os.path import Image import hashlib def get_md5(filename): m = hashlib.md5() mfile = open(filename, "rb") m.update(mfile.read()) mfile.close() md5_value = m.hexdigest() return md5_value if __name__ == '__main__': ipath = "E:\\20161019_photo" uipath = unicode(ipath, "utf8") for parent, dirnames, filenames in os.walk(uipath): md5_list = [] #for dirname in dirnames: # 输出文件夹信息 #print "parent is:" + parent #print "dirname is: " + dirname for filename in filenames: #print "parent is :" + parent #print "filename is:" + filename #print "md5_list is : " if(get_md5(os.path.join(parent, filename)) in md5_list): os.remove(os.path.join(parent, filename)) else: md5_list.append(get_md5(os.path.join(parent, filename))) #print md5_list #md5 = []
删除无法打开文件:
#coding=utf-8 import os import os.path import Image import hashlib def get_md5(filename): m = hashlib.md5() mfile = open(filename, "rb") m.update(mfile.read()) mfile.close() md5_value = m.hexdigest() return md5_value if __name__ == '__main__': ipath = "E:\\20161019_photo" uipath = unicode(ipath, "utf8") for parent, dirnames, filenames in os.walk(uipath): for filename in filenames: name ,ext = os.path.splitext(filename) if((ext == ".png") or (ext == ".tif") or (ext == ".gif")): os.remove(os.path.join(parent, filename)) else: if(ext == ".jpg"): try: fp = open(os.path.join(parent, filename), 'rb') img = Image.open(fp) except: fp.close() os.remove(os.path.join(parent, filename)) else: continue
本文python删除文件夹下相同文件和无法打开的图片到此结束。成功人的背后总会有一个不寻常的故事。小编再次感谢大家对我们的支持!