Possible File iteration bug

Billy Mays noway at nohow.com
Thu Jul 14 22:15:42 CEST 2011


On 07/14/2011 04:00 PM, Ian Kelly wrote:
> 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.

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

--
Bill



More information about the Python-list mailing list