mixing for x in file: and file.readline

John J. Lee jjl at pobox.com
Thu Sep 11 19:06:08 EDT 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:

http://www.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=87llukrdow.fsf%40pobox.com&rnum=1&prev=/groups%3Fas_q%3D%2522for%2520line%2520in%2520file%253A%2522%2520group%253Acomp.lang.python%26safe%3Dimages%26ie%3DISO-8859-1%26as_uauthors%3Djjl%40pobox.com%26lr%3D%26num%3D30%26hl%3Den

http://tinyurl.com/n2cc



> 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).


John




More information about the Python-list mailing list