python删除文件夹下相同文件和无法打开的图片
作者:应澜lst 时间:2023-03-09 19:26:42
前天不小心把硬盘格式化了,丢了好多照片,后来用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
来源:https://blog.csdn.net/lst227405/article/details/52877662
标签:python,删除文件,删除图片
0
投稿
猜你喜欢
python实现折半查找和归并排序算法
2023-06-24 00:08:01
困惹的A标签
2007-12-04 12:36:00
动态SQL中返回数值的实现代码
2012-01-05 18:53:54
用Python实现服务器中只重载被修改的进程的方法
2022-06-21 05:11:38
jQuery+php简单实现全选删除的方法
2023-11-05 20:23:38
Python 剪绳子的多种思路实现(动态规划和贪心)
2021-07-03 18:43:41
mysql 字符串正则表达式及说明
2024-01-13 17:47:59
oracle chm帮助文件下载
2024-01-21 21:08:01
mysql优化的重要参数 key_buffer_size table_cache
2024-01-16 03:01:52
解析PHP观察者模式Observer
2023-07-08 13:38:58
vue文本识别"\\n"换行问题的解决方式
2024-05-09 10:51:43
部署Django到阿里云服务器教程示例
2022-03-28 23:46:19
vscode配置远程开发与免密登录的技巧
2023-04-29 02:43:41
python中dump与dumps实现序列化
2023-01-13 22:46:16
tensorflow指定GPU与动态分配GPU memory设置
2023-01-16 11:30:24
浅谈MySQL timestamp(3)问题
2024-01-14 17:47:23
跟老齐学Python之变量和参数
2023-01-12 22:23:31
JS实现简单的二元方程计算器功能示例
2024-05-02 17:39:30
nx.adjacency_matrix计算邻接矩阵与真实结果不一致的解决
2023-12-06 14:23:56
vue中@keyup.enter失效问题及解决
2023-07-02 17:01:38