Possible File iteration bug

Terry Reedy tjreedy at udel.edu
Thu Jul 14 16:43:12 EDT 2011


On 7/14/2011 3:46 PM, Billy Mays wrote:
> I noticed that if a file is being continuously written to, the file
> generator does not notice it:

Because it does not look, as Ian explained.

> def getLines(f):
> lines = []
> for line in f:
> lines.append(line)
> return lines

This nearly duplicates .readlines, except for using f an an iterator.
Try the following (untested):

with open('/var/log/syslog', 'rb') as f:
   lines = f.readlines()
   # do some processing with lines
   # /var/log/syslog gets updated in the mean time
   lines = f.readlines()

People regularly do things like this with readline, so it is possible. 
If above does not work, try (untested):

def getlines(f):
   lines = []
   while True:
     l = f.readline()
     if l: lines.append(l)
     else: return lines

-- 
Terry Jan Reedy




More information about the Python-list mailing list