[Python-ideas] Is there a reason why file.readlines() doesn't/can't return an iterator?

Peter Otten __peter__ at web.de
Wed Nov 30 18:08:56 CET 2011


Éric Araujo wrote:

> Good point.  readlines strips the end-of-line characters though, so to
> use list(fp) I’d have to change all my expected outputs to add \n.

No, you seem to be confusing what_used_to_be_file.readlines() with 
str.splitlines().

>>> open("tmp.txt").readlines()
['alpha\n', 'beta\n', 'gamma\n']
>>> open("tmp.txt").read().splitlines()
['alpha', 'beta', 'gamma']





More information about the Python-ideas mailing list