Questions
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 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 would an elegant solution to this problem look like?
Answers
As of Python 2.7 (or 3.1 respectively) you can write
with open( a , w ) as a, open( b , w ) as b:
do_something()
In earlier versions of Python, you can sometimes use
http://docs.python.org/library/contextlib.html#contextlib.nested
http://docs.python.org/library/contextlib.html#contextlib.nested
Source
License : cc by-sa 3.0
http://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python
Related