idiomatic analogue of Perl's: while (<>) { ... }

Peter Otten __peter__ at web.de
Thu Sep 1 03:43:04 EDT 2011


Peter Otten wrote:

> A quick look into fileinput.py reveals that it uses readlines() and slurps
> in the complete "file". I'm not sure that was a clever design decision...

Correction: 

>>> with open("tmp.txt") as f: lines = f.readlines(0)
...
>>> len(lines)
1000000
>>> with open("tmp.txt") as f: lines = f.readlines(1)
...
>>> len(lines)
301
>>> len("".join(lines))
8208

So on my system file.readlines(size) stops after about 2**13 bytes which is 
not a problem memorywise. Sorry for the confusion.




More information about the Python-list mailing list