Difference between readlines() and iterating on a file object?
Michael Hudson
mwh at python.net
Fri Aug 13 11:04:44 EDT 2004
Duncan Booth <duncan.booth at invalid.invalid> writes:
> "Richard" <richardd at hmgcc.gov.uk> wrote in
> news:411cd102$1 at mail.hmgcc.gov.uk:
>
> > Hi,
> >
> > Can anyone tell me what the difference is between
> >
> > for line in file.readlines( ):
>
> reads the entire file into memory and splits it up into a list of lines
> then iterates over the list. If you break from the loop, tough you've lost
> any lines that were read but you didn't handle.
>
> >
> > and
> >
> > for line in file:
>
> reads part of the file and strips off one line at a time. Never creates a
> list. Reads more only when it runs out of the block it read. If you break
> from the loop you can do another 'for line in file' and get the remaining
> lines.
But this last part only works the way you expect in 2.3, I think.
Cheers,
mwh
--
Ability to type on a computer terminal is no guarantee of sanity,
intelligence, or common sense.
-- Gene Spafford's Axiom #2 of Usenet
More information about the Python-list
mailing list