前天不小心把硬盘格式化了,丢了好多照片,后来用Recuva这款软件成功把文件恢复过来,可
前天不小心把硬盘格式化了,丢了好多照片,后来用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删除文件夹下相同文件 python删除无法打开图片 python删除相同文件