Skip to main content
use with statement to close the file always
Source Link
jfs
  • 417.1k
  • 210
  • 1k
  • 1.7k

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')) 

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 = open(filename, mode='rb') d = hashlib.md5() for buf in iter(partial(f.read, 128), b''): d.update(buf) return d.hexdigest() print(md5sum('utils.py')) 

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): 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')) 
Source Link
Raymond Hettinger
  • 228.8k
  • 67
  • 405
  • 504

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 = open(filename, mode='rb') d = hashlib.md5() for buf in iter(partial(f.read, 128), b''): d.update(buf) return d.hexdigest() print(md5sum('utils.py'))