admin管理员组文章数量:1660166
整理数据,发现有些文件名虽然不一样,但是文件内容是一样的。所以使用MD5值进行查重,然后通过os.system调用shell脚本删除多余元素。
#code by yangdaxia
import hashlib
import os
from collections import Counter
def getFileMD5(filepath):
'''
:param filepath:
:return: md5
'''
f = open(filepath,'rb')
md5 = hashlib.md5()
md5.update(f.read())
hash = md5.hexdigest()
f.close()
return str(hash)
def checkRemove(path):
files = []
fileMd5s = []
lines = os.listdir(path)
for idx, file in enumerate(lines):
Md5 = getFileMD5(path + file)
fileMd5s.append(Md5)
#print(idx, yl)
files.append(file)
tmp = dict(zip(files, fileMd5s))
print('构建文件与MD5映射完毕!')
chachong = Counter(fileMd5s)
print('查重中......')
for key, value in chachong.items():
if int(value) >1:
print(key, value)
chongfu = [key2 for key2, value2 in tmp.items() if value2 ==key]
print(chongfu)
with open('chongfu.txt', 'a')as fw:
fw.write(str(chongfu)[1:-1]+'\n')
rmf = path+chongfu[-1]
print(rmf)
os.system('rm -v %s'%(rmf))
if __name__=='__main__':
path = '/mnt/lustrenew/dataset/test/Image/guonei_test/Images/'
checkRemove(path)
版权声明:本文标题:利用文件的md5值查重并删除重复的文件 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729845452a1215075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论