81-计算文件的MD5值

时间:2021-07-11 10:10:18

用于查看文件的md5值:

import hashlib
import sys

def check_md5(fname):
    m = hashlib.md5()

    with open(fname, 'rb') as fobj:
        while True:
            data = fobj.read(4096)
            if not data:
                break
            m.update(data)

    return m.hexdigest()

if __name__ == '__main__':
    print(check_md5(sys.argv[1]))  # python3 check_md5.py /etc/passwd

结果输出:

hejianping@ubuntu:~$ python3 check_md5.py /etc/passwd
9df614da77b1dccfc905b3ec4fdb9db7