DEV Community


Posted on

A neat Python buffer read iterable.

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!
Enter fullscreen mode Exit fullscreen mode

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:, ''):
        # Super-cool stuff using a 1K buffer!!
Enter fullscreen mode Exit fullscreen mode

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?

iter function documentation

Top comments (0)