前天不小心把硬盘格式化了,丢了好多照片,后来用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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!