Possible File iteration bug

Hrvoje Niksic hniksic at xemacs.org
Thu Jul 14 22:39:52 CEST 2011

Billy Mays <noway at nohow.com> writes:

> Is there any way to just create a new generator that clears its
> closed` status?

You can define getLines in terms of the readline file method, which does
return new data when it is available.

def getLines(f):
    lines = []
    while True:
        line = f.readline()
        if line == '':
    return lines

or, more succinctly:

def getLines(f):
    return list(iter(f.readline, ''))

More information about the Python-list mailing list