• 以rb模式读取文件
  • 文件较大时可分块读取 ```python

    !/usr/bin/env python

    -- encoding: utf8 --

    import hashlib

def md5sum(filename, size=4096):

  1. md5 = hashlib.md5()
  2. with open(filename, 'rb') as f:
  3. while True:
  4. b = f.read(size)
  5. if not b:
  6. break
  7. md5.update(b)
  8. return md5.hexdigest()

if name == ‘main‘:

  1. import sys
  2. if len(sys.argv) < 2:
  3. print 'usage: %s <infile>' % sys.argv[0]
  4. exit()
  5. infile = sys.argv[1]
  6. md5_value = md5sum(infile)
  7. print '{} {}'.format(md5_value, infile)

``` 运行结果
image.png