looping over a big file
Roy Smith
roy at panix.com
Sun Jul 3 18:02:19 EDT 2005
"martian" <nospam at hetnet.nl> wrote:
> 1) how does python handle:
>
> > for line in big_file:
>
> is big_file all read into memory or one line is read at a time or a buffer
> is used or ...?
The "right" way to do this is:
for line in file ("filename"):
whatever
The file object returned by file() acts as an iterator. Each time through
the loop, another line is read and returned (I'm sure there is some
block-level buffering going on at a low level).
> 2) is it possible to advance lines within the loop? The following doesn't
> work:
>
> > for line in big_file:
> line_after = big_file.readline()
You probably want something like:
for line in file ("filename"):
if skipThisLine:
continue
More information about the Python-list
mailing list