如何在Python中使用“ with open”打开多个文件?

人盡茶涼 提交于 2020-08-09 02:13:02

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!