import sys, hashlib
import os
inputFile = 'C:\Users\User\Desktop\hashes.txt'
sourceDir = 'C:\Users\User\Desktop\Test Directory'
hashMatch = False
for root, dirs, files in os.walk(sourceDir):
for filename in files:
sourceDirHashes = hashlib.md5(filename)
for digest in inputFile:
if sourceDirHashes.hexdigest() == digest:
hashMatch = True
break
if hashMatch:
print str(filename)
else:
print 'hash not found'inputFile的内容=
2899ebdb5f7a90a216e97b3187851fc1
54c177418615a90a6424cb945f7a6aec
dd18bf3a8e0a2a3e53e2661c7fb53534sourceDir文件的内容=
test
test 1
test 2我发布的当前代码总是返回else语句,即没有找到散列,即使它们在我验证这一点时也是如此。我已经提供了我的sourceDir的内容,以便有人尝试这个,文件名是test,test 1和TEST2,相同的内容在文件中。
但是,我必须补充的是,我不是在寻找打印实际文件内容的脚本,而是在寻找文件的名称。
发布于 2016-03-25 08:13:43
您需要使用open( inputFile,'rt')打开inputFile,然后才能读取散列。此外,在读取散列时,请确保先将其剥离,以去除行尾的换行符\n
https://stackoverflow.com/questions/36212100
复制相似问题