圆月山庄资源网 Design By www.vgjia.com

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

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com