I've recently discovered a Python "nifty" regarding iter(). We've seen this form of iterable file read a million times:
with open('eek.txt', 'rt') as f:
for line in f:
# Some super-cool stuff!
But!! Did you know you can read arbitrary-length buffers in a similar form? Use the iter(object, sentinel) form of the call and you can do this:
with open('eek.txt', 'rt') as f:
# Use whatever chunk size you want
for buff in iter(lambda: f.read(1024), ''):
# Super-cool stuff using a 1K buffer!!
The use of iter(read_chunk, '') is basically saying: "Call read_chunk until you get an empty string." This could be done with binary files as well. The mode would be 'rb' and the sentinel would be b''.
Neato, eh?
Top comments (0)