Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

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 other way to solve this problem look like?

1 Answer

0 votes
by (25.1k points)
edited by

You are using the syntax incorrectly here which is why you are getting the error. You need to separate these two 'open' statements using a comma ',' not an 'and'. Like this:

with open('a', 'w') as a and open('b', 'w') as b:

    do_something()

If you wish to get an in-depth understanding about python in general you should check out this video:

Related questions

+2 votes
2 answers
0 votes
1 answer
asked Sep 24, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...