Possible File iteration bug

Hrvoje Niksic hniksic at xemacs.org
Thu Jul 14 16:39:52 EDT 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 == '':
            break
        lines.append(line)
    return lines

or, more succinctly:

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



More information about the Python-list mailing list