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:-.
with open('a', 'w') as a, open('b', 'w') as b:
except IOError as e:
print('Operation failed: %s' % e.strerror)
To know more about this you can have a look at the following video:-