找出代码块失败后报表文件仍保持打开的原因。
Python
from contextlib import contextmanager
@contextmanager
def open_report(path):
handle = open(path, "w", encoding="utf-8")
yield handle
handle.close()
try:
with open_report("report.txt") as report:
report.write("draft")
raise ValueError("invalid report")
except ValueError:
print(report.closed)