You are almost certainly missing the second argument to the iter()
function.
Without the second argument the first argument must be iterable, but a lambda
produces a function, and that's not an iterable:
>>> iter(lambda: f.read(65536))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'function' object is not iterable
From the iter()
documentation:
The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the __iter__()
method), or it must support the sequence protocol (the __getitem__()
method with integer arguments starting at 0
). If it does not support either of those protocols, TypeError
is raised.
With the second argument there you get a proper iterator for the for
loop to iterate over:
>>> iter(lambda: f.read(65536), '')
<callable-iterator object at 0x100563150>
Each iteration the lambda function is called, until that function returns ''
, the empty string. In other words, 64Kb is read each iteration, until the end of the file is reached, at which point the f.read()
operation returns an empty string and the iterator raises StopIteration
.