try:
    with open('a', 'w') as a:
        with open('b', 'w') as b:
            do_something()
except IOError as e:
    print('Operation failed: %s' % e.strerror)
How can I open multiple files using "with open" in Python?