DEV Community

HAP
HAP

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: f.read(1024), ''):
        # 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)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay