我正在尝试确定一些文件的数据一致性。然而,MD5 值一直会出现不同。当我执行 md5sum
时,哈希值是相等的:
import hashlib
import os
import sys
def hash_file_content(path):
try:
if not os.path.exists(path):
raise IOError, "File does not exist"
encode = hashlib.md5(path).hexdigest()
return encode
except Exception, e:
print e
def main():
hash1 = hash_file_content("./downloads/sample_file_1")
hash2 = hash_file_content("./samples/sample_file_1")
print hash1, hash2
if __name__ == "__main__":
main()
输出结果出现了意外的差异:
baed6a40f91ee5c44488ecd9a2c6589e 490052e9b1d3994827f4c7859dc127f0
现在使用 md5sum
:
md5sum ./samples/sample_file_1
9655c36a5fdf546f142ffc8b1b9b0d93 ./samples/sample_file_1
md5sum ./downloads/sample_file_1
9655c36a5fdf546f142ffc8b1b9b0d93 ./downloads/sample_file_1
为什么会发生这种情况,我该如何解决?