I think you wanted the for-loop to make successive calls to f.read(128). That can be done using iter() and functools.partial():
import hashlib from functools import partial def md5sum(filename): f =with open(filename, mode='rb') as f: d = hashlib.md5() for buf in iter(partial(f.read, 128), b''): d.update(buf) return d.hexdigest() print(md5sum('utils.py'))