来源于 http://wuqinzhong.blog.163.com/blog/static/4522231200942225810117/

 

#coding:utf-8
#python 检测文件MD5值
#python version 2.6

import hashlib
import os

#简单的测试一个字符串的MD5值
src = 'teststring'
print (hashlib.md5(src).hexdigest().upper())
#hexdigest() 为十六进制值,digest()为二进制值

#使用update
m0=hashlib.md5()
m0.update(src)
print m0.hexdigest().upper()

#一个小文件的MD5值
filename = 'c:\\boot.ini'
f = file(filename,'rb')
m1 = hashlib.md5()
m1.update(f.read(8096))
print m1.hexdigest().upper()
f.close()

#大文件的MD5值
def GetFileMd5(filename):
    if not os.path.isfile(filename):
        return
    myhash = hashlib.md5()
    f = file(filename,'rb')
    while True:
        b = f.read(8096)
        if not b :
            break
        myhash.update(b)
    f.close()
    return myhash.hexdigest().upper()

print GetFileMd5("c:\\a.rar")

相关文章:

  • 2022-12-23
  • 2021-06-27
  • 2022-01-24
  • 2021-05-17
  • 2021-10-12
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2022-12-23
相关资源
相似解决方案