[Python] How can I open multiple files using "with open" in Python?

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I somehow can combine the multiple open calls with the with statement:

    try:
    with open('a', 'w') as a and open('b', 'w') as b:
    do_something()
    except IOError as e:
    print 'Operation failed: %s' % e.strerror


    If that's not possible, what would an elegant solution to this problem look like?

    Continue reading...

Compartilhe esta Página