【发布时间】:2019-10-06 16:21:00
【问题描述】:
我想知道 Python 3 中是否存在允许我在同一个 with 上下文中打开两个(或更多)文件的结构。
我正在寻找的是这样的:
from pathlib import Path
file1 = Path('file1.txt')
file2 = Path('file2.txt')
with file1.open() as f1 and file2.open() as f2:
'''do something with file handles...
上面的代码显然是无效的,这就引出了这个问题。
【问题讨论】:
-
试试
and→,
标签: python python-3.x with-statement