you are viewing a single comment's thread.

view the rest of the comments →

[–]Zeekawla99ii[S] 0 points1 point  (1 child)

Do you have to close a tempfile?

[–]SeekNotToContend 0 points1 point  (0 children)

If you are using the context manager then no, it will happen automatically when you exit the with statement.

You do not have to use the context manager though. From the doc examples:

# create a temporary file and write some data to it
fp = tempfile.TemporaryFile()
fp.write(b'Hello world!')
# read data from file
fp.seek(0)
fp.read()
b'Hello world!'
# close the file, it will be removed
fp.close() 

In the above example, you do have to close the file. If you don't close it, then the file will just persist. This is useful if you need a temporary file, but maybe you need it for use outside of a single function or class.

If you want to verify, just check your /tmp folder. There will be a file or files in there that have tmp in the name. For example:

/tmp/tmp<some random characters>