python Delete the same files and pictures that cannot be opened under the folder
- 2021-07-22 10:01:06
- OfStack
The day before yesterday I accidentally formatted the hard disk and lost a lot of photos. Later I used Recuva this software to restore the files successfully, but there are many duplicate files and pictures that can't be opened in the recovered files, so I wrote two small programs of python to solve this problem
Delete the same file:
#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: # Output folder information
#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 = []
Delete Unable to Open File:
#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