【发布时间】:2013-08-27 06:28:17
【问题描述】:
对我来说,调用tempfile.mkstemp() 最惯用的方式是:
with tempfile.mkstemp() as fd, filename:
pass
然而,这显然(?)引发AttributeError: __exit__
使用 try-finally 显式调用 os.close(fd) 是解决此问题的一种简单方法,但感觉违反了应该有一种——最好只有一种——明显的方法。
有没有办法在tempfile 中“修复”这个问题,或者有理由为什么以这种方式实施?
【问题讨论】:
-
感谢您的反对,请评论如何改进问题。
标签: python with-statement temporary-files python-internals