def md5sum(filename):
    with open(filename, mode='rb') as f:
        d = hashlib.md5()
while True:
    buf = f.read(4096)
if not buf:
    break
d.update(buf)
return d.hexdigest()
Using hashlib to compute md5 digest of a file in Python 3