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

Éric Araujo merwok at netwok.org
Wed Nov 30 17:23:16 CET 2011


Le 30/11/2011 17:18, Masklinn a écrit :
> On 2011-11-30, at 16:51 , Éric Araujo wrote:
>> You don’t always read big files, or you may prefer to
>> close the file handle early at the cost of memory, or you need a list to
>> do some processing that works with lists but not iterators.  FWIW, I use
>> readlines a lot to get list of lines for unit tests, for example (in
>> case of failure, lists make better diffs than strings).
> Then again, the same process should be used here than the one recommended
> for map, filter, dict.(keys|values|items) in Python 3: `list(file)` will
> yield a list of lines.

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.

Cheers



More information about the Python-ideas mailing list