Warning about "for line in file:"

Aldo Cortesi aldo at nullcube.com
Wed Feb 20 20:51:52 EST 2002


Thus spake Neil Schemenauer (nas at python.ca):

> Jason Orendorff wrote:
> > I agree!  This could be improved by having
> > file.__iter__() always return the same object.  But that
> > doesn't solve the problem in full.
> 
> Perhaps the buffer should be attached to the file object
> itself instead of to the iterator.  read() and readline()
> could then be changed to use the buffer if it exists.

... but this would leave a discrepancy between the actual
file position and the notional "read" position. Among other
things, file.tell() would then give the wrong information,
and file.write() would write to an unexpected location. 

It looks like the only complete solution (short of changing
the sematics by fiat) would be to keep the file descriptor
offset correct by performing a seek after reading every
line. Then again, this would probably kill any efficiencies
gained by xreadlines...



Cheers,


Aldo



-- 
Aldo Cortesi
aldo at nullcube.com
www.nullcube.com


-- 
Aldo Cortesi
aldo at nullcube.com
www.nullcube.com




More information about the Python-list mailing list