an ugly file-reading pattern

Christian Tanzer tanzer at swing.co.at
Mon Apr 14 03:08:02 EDT 2003


Skip Montanaro <skip at pobox.com> wrote:

> Recent versions of Python (2.2 and later) allow this:
>
>     file = file("somefile")
>     for line in file:
>         process(line)
>
> It does the right thing from an efficiency standpoint, neither slurping the
> file in all at once nor reading it line-by-line or char-by-char.
>
> Note also that in 2.2 and later there is a file object in builtins ("open"
> and "file" are the same object).  "open" is discouraged.  "file" is
> preferred.

Maybe you should have written it as :

    open = file("somefile")
    for line in open:
        process(line)

<2.2 wink>

Hiding-builtins-often-bytes-the-unwary-ly yr's,

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list