Warning about "for line in file:"

Just van Rossum just at xs4all.nl
Mon Feb 18 11:33:07 CET 2002


Russell E. Owen wrote:

> I tried taking advantage of
>   for line in file:
> (a recent addition to the language) and ran into some odd behavior.
> 
> I read some lines using one for loop, to get to the start of the data of 
> interest. E.g.:
> for line in file:
>   if startingcondition(line):
>     break
> 
> I then some more lines from the same file using a similar for loop, but 
> found that the lines were not contiguous! Instead there was a huge gap 
> between the two sets of reads, as if the first for loop read in a lot of 
> extra data that it never spit out.
> 
> [ ... ]

It took me a while to understand what you meant, and the followups 
didn't make it much clearer either... But now that I do, I'm just as 
baffled as you are...

Here's what happens on my box:

  >>> f = open("tmp.txt", "w")
  >>> for i in range(10000):
  ...   f.write("%s\n" % i)
  ... 
  >>> f.close()
  >>>
  >>> f = open("tmp.txt")
  >>> for line in f:
  ...   print line.strip()
  ...   break
  ... 
  0
  >>> for line in f:
  ...   print line.strip()
  ...   break
  ... 
  1861
  >>> 

This is highly unintuitive to me, indeed so much so that I would like to 
call it a bug!

Just



More information about the Python-list mailing list