mixing for x in file: and file.readline

Russell E. Owen rowen at cesmail.net
Thu Sep 11 22:54:53 CEST 2003

At one time, mixing for x in file and readline was dangerous. For 

for line in file:
  # read some lines from a file, then break
nextline = readline() # bad

would not do what a naive user might expect because the file iterator 
buffered data and readline did not read from that buffer. Hence the call 
to readline might unexpectedly skip some lines.

I stumbled across this the hard way, but am wondering if it's still 
present in Python 2.3. I thought I'd seen it documented recently, but 
looking through the description of the file object in the Python Library 
Reference, I didn't see it.

Anyone know if it's still an issue? If so, anyone have any idea how hard 
it would be to fix? I'm willing to work on a patch, but would probably 
need some help. And if experts have already determined it's too hard, 
and are willing to expain, I'd love some idea of why that is.

-- Russell

More information about the Python-list mailing list