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.read
would read the whole file, since thesize
parameter is missing; f.read(1024)
means call a function and pass its return value (data loaded from file) toiter
, soiter
does not get a function at all;(lambda:f.read(1234))
is a function that takes zero arguments (nothing betweenlambda
and:
) and callsf.read(1234)
.