I have the following script, which does the same thing twice, in very slightly different ways. The first works, the second does not:
#!/usr/bin/python import tempfile fhandle=tempfile.NamedTemporaryFile(dir=".",delete=False) fhandle.write("hello") tempfile.NamedTemporaryFile(dir=".",delete=False).write("hello") I get the follow error:
Traceback (most recent call last): File "./test.py", line 7, in <module> tempfile.NamedTemporaryFile().write("hello") ValueError: I/O operation on closed file In my example script, I have put them together to show that the first one works. This does not affect the results, just points out that there is a difference.
Is this a bug in Python? Something weird about my machine? Expected behaviour? Correct behaviour? It looks like the object is being destroyed before the write().
Python 2.7.3 on Ubuntu 12.04.3 LTS
NamedTemporaryFile. Why would you want do this?__del__somehow gets called on the result ofNamedTemporaryFilebefore the call towriteoccurs. Why that would happen, I do not know.