Possible File iteration bug

Ian Kelly ian.g.kelly at gmail.com
Thu Jul 14 16:00:06 EDT 2011


On Thu, Jul 14, 2011 at 1:46 PM, Billy Mays <noway at nohow.com> wrote:
> def getLines(f):
>    lines = []
>    for line in f:
>        lines.append(line)
>    return lines
>
> with open('/var/log/syslog', 'rb') as f:
>    lines = getLines(f)
>    # do some processing with lines
>    # /var/log/syslog gets updated in the mean time
>
>    # always returns an empty list, even though f has more data
>    lines = getLines(f)
>
>
>
>
> I found a workaround by adding f.seek(0,1) directly before the last
> getLines() call, but is this the expected behavior?  Calling f.tell() right
> after the first getLines() call shows that it isn't reset back to 0.  Is
> this correct or a bug?

This is expected.  Part of the iterator protocol is that once an
iterator raises StopIteration, it should continue to raise
StopIteration on subsequent next() calls.



More information about the Python-list mailing list