Try:
chunk_size = 4 * 1024 * 1024 # MB
with open('large_file.dat','rb') as f:
for chunk in iter(lambda: f.read(chunk_size), b''):
handle(chunk)
iter needs a function with zero arguments.
- a plain
f.readwould read the whole file, since thesizeparameter is missing; f.read(1024)means call a function and pass its return value (data loaded from file) toiter, soiterdoes not get a function at all;(lambda:f.read(1234))is a function that takes zero arguments (nothing betweenlambdaand:) and callsf.read(1234).