Actually, you are following the correct way to open multiple files in Python in the only thing you need to do is, replace ‘and’ with ‘,’ and you are done with your answer:-.
try:
with open('a', 'w') as a, open('b', 'w') as b:
do_something()
except IOError as e:
print('Operation failed: %s' % e.strerror)
To know more about this you can have a look at the following video:-