How can I open multiple files using “with open” in Python?

后端 未结 7 961
天涯浪人
天涯浪人 2020-11-22 08:15

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

7条回答
  •  借酒劲吻你
    2020-11-22 09:02

    Just replace and with , and you're done:

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

提交回复
热议问题