mixing for x in file: and file.readline

John J. Lee jjl at pobox.com
Fri Sep 12 01:06:08 CEST 2003

"Russell E. Owen" <rowen at cesmail.net> writes:

> At one time, mixing for x in file and readline was dangerous. For 
> example:
> 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.

There was a thread-fragment about this a while back.  See the message
from Steven Taschuk a few messages past this one:



> Anyone know if it's still an issue? If so, anyone have any idea how hard 

Was fixed in 2.3, maybe in 2.2.3 also (not sure).


More information about the Python-list mailing list