次のスクリプトは、同じことをわずかに異なる方法で 2 回実行します。前者は機能しますが、後者は機能しません。
#!/usr/bin/python
import tempfile
fhandle=tempfile.NamedTemporaryFile(dir=".",delete=False)
fhandle.write("hello")
tempfile.NamedTemporaryFile(dir=".",delete=False).write("hello")
次のエラーが表示されます。
Traceback (most recent call last):
File "./test.py", line 7, in <module>
tempfile.NamedTemporaryFile().write("hello")
ValueError: I/O operation on closed file
サンプル スクリプトでは、最初のスクリプトが機能することを示すためにそれらをまとめました。これは結果には影響しません。違いがあることを指摘するだけです。
これは Python のバグですか? 私のマシンに何か変なところがありますか?予想される動作?正しい行動?write() の前にオブジェクトが破棄されているようです。
Ubuntu 12.04.3 LTS 上の Python 2.7.3