问题:
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: 我想知道我是否可以将多个打开调用与with
语句结合with
:
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? 如果不可能,那么解决该问题的优雅解决方案会是什么样?
解决方案:
参考一: https://stackoom.com/question/JN6I/如何在Python中使用-with-open-打开多个文件参考二: https://oldbug.net/q/JN6I/How-can-I-open-multiple-files-using-with-open-in-Python
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4274541