python - 文件md5校验

通过md5校验文件,我们可以判断文件内容是否改变,用python实现代码如下

import hashlib

# 1.txt为空文本
with open("1.txt", "rb") as f:
    data = f.read()
    md5Old = hashlib.md5(data).hexdigest()
    print(md5Old)  # d41d8cd98f00b204e9800998ecf8427e

# 1.txt内容为123
with open("1.txt", "rb") as f:
    data = f.read()
    md5New = hashlib.md5(data).hexdigest()
    print(md5New)  # 202cb962ac59075b964b07152d234b70

posted @ 2023-08-23 21:25  wstong  阅读(275)  评论(0编辑  收藏  举报