Python内置hashlib模块来获取文件MD5值

admin Python评论282字数 264阅读模式
1. 导入hashlib模块
import hashlib

2. 打开文件并读取内容
with open('file.txt', 'rb') as f:
    content = f.read()

3. 计算MD5值
md5 = hashlib.md5(content).hexdigest()

完整代码如下:

import hashlib
with open('file.txt', 'rb') as f:
    content = f.read()
md5 = hashlib.md5(content).hexdigest()
print(md5)

其中,'file.txt'为要获取MD5值的文件路径。

版权声明:文章图片资源来源于网络,如有侵权,请留言删除!!!
admin
  • 本文由 发表于 2023年5月13日 17:37:29
  • 转载请务必保留本文链接:https://www.58pxe.com/10599.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: