I am creating a tmp file by using :
from tempfile import mkstemp
I am trying to write in this file :
tmp_file = mkstemp()
file = open(tmp_file, 'w')
file.write('TEST\n')
Indeed I close the file and do it proper but when I try to cat the tmp file, it stills empty..It looks basic but I don't know why it doesn't work, any explanations ?
This example opens the Python file descriptor with
os.fdopen
to write cool stuff, then close it (at the end of thewith
context block). Other non-Python processes can use the file. And at the end, the file is deleted.The answer by smarx opens the file by specifying
path
. It is, however, easier to specifyfd
instead. In that case the context manager closes the file descriptor automatically:mkstemp()
returns a tuple with a file descriptor and a path. I think the issue is that you're writing to the wrong path. (You're writing to a path like'(5, "/some/path")'
.) Your code should look something like this: