Possible File iteration bug

Billy Mays noway at nohow.com
Fri Jul 15 08:26:21 EDT 2011

On 07/15/2011 04:01 AM, bruno.desthuilliers at gmail.com wrote:
> On Jul 14, 9:46 pm, Billy Mays<no... at nohow.com>  wrote:
>> I noticed that if a file is being continuously written to, the file
>> generator does not notice it:
>> def getLines(f):
>>       lines = []
>>       for line in f:
>>           lines.append(line)
>>       return lines
> what's wrong with file.readlines() ?

Using that will read the entire file into memory which may not be 
possible.  In the library reference, it mentions that using the 
generator (which calls file.next()) uses a read ahead buffer to 
efficiently loop over the file.  If I call .readline() myself, I forfeit 
that performance gain.

I was thinking that a convenient solution to this problem would be to 
introduce a new Exception call PauseIteration, which would signal to the 
caller that there is no more data for now, but not to close down the 
generator entirely.


More information about the Python-list mailing list