mixing for x in file: and file.readline

John J. Lee jjl at pobox.com
Fri Sep 12 10:14:57 EDT 2003


Oren Tirosh <oren-py-l at hishome.net> writes:

> On Thu, Sep 11, 2003 at 01:54:53PM -0700, Russell E. Owen wrote:
> > At one time, mixing for x in file and readline was dangerous. For 
> > example:
[...]
> Yes.
[...]
> In Python 2.2 if you break in the middle of the loop the temporary 
> iterator object (xreadlines) is lost along with its readahead buffer, 
> leaving you at an unknown file position. The only things you can do are 
> to close the file or seek. In Python 2.3 the file object IS an iterator 
> (rather than HAS and iterator) so while the current file position is 
> undefined from a read/readline/tell point of view the iterator state is
> still consistent so you can immediately use it in another for loop to 
> continue from the same position or even call its next() method directly.
[...]

Oh, sorry for the misinformation -- I thought the repeated-iteration
and mixing-iteration-with-readline issues were the same, but clearly
not.


John




More information about the Python-list mailing list