[Tutor] Appending an extra column in a data file

eryksun eryksun at gmail.com
Wed Apr 10 23:17:00 CEST 2013


On Wed, Apr 10, 2013 at 4:45 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
>
>> with open('old.dat') as fin, open('new.dat', 'w') as fout:
>
> Is that Python 3.x?

It's the same syntax as an import statement such as the following:

    import numpy as np, matplotlib as mpl

A downside with this is the inability to split long lines by adding
parentheses. Obviously you can break at the open() call:

    with open('old.dat') as fin, open(
     'new.dat', 'w') as fout:
        for line in fin:

But I think it's more readable to break up a long line like this:

    fin = open('old.dat')
    fout = open('new.dat', 'w')

    with fin, fout:
        for line in fin:


More information about the Tutor mailing list