Which of the following is more correct?
fi, path = tempfile.mkstemp()
f = os.fdopen(fi, "w")
f.write(res)
f.close()
os.close(fi)
or:
fi, path = tempfile.mkstemp()
f = os.fdopen(fi, "w")
f.write(res)
f.close()
Check f.fileno()
, it should be the same as fi
. You should only ever close that file descriptor once, so the second is correct.
On Unix, the first causes an error:
>>> f.close()
>>> os.close(fi)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With