looping over a big file

Mike Meyer mwm at mired.org
Sun Jul 3 18:36:24 EDT 2005


Roy Smith <roy at panix.com> writes:
> 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).

I disagree. That's the *convenient* way to do it, and perfectly
acceptable in many situations. But not all Python interpreters will
close the file when for loop ends. Likewise, if you get an exception
during the processing, the file may not get closed properly. Those
things may matter to you, in which case the "right" way is:

    data = open("filename")
    try:
        for line in data:
            whatever
    finally:
        data.close()

Guido has made a pronouncement on open vs. file. I think he prefers
open for opening files, and file for type testing, but may well be
wrong. I don't think it's critical.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list