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)

Learn How Clay Overcame Web Scraping Barriers

Learn How Clay Overcame Web Scraping Barriers

Clay helps customers enrich their sales pipeline with data from even the most protected sites. Discover how Clay overcame initial limitations and scaled past data extraction bottlenecks with a boost from ZenRows.

Read More

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay