I want to use tempfile.NamedTemporaryFile()
to write some contents into it and then open that file. I have written following code:
tf = tempfile.Nam
You can also use it with a context manager so that the file will be closed/deleted when it goes out of scope. It will also be cleaned up if the code in the context manager raises.
import tempfile
with tempfile.NamedTemporaryFile() as temp:
temp.write('Some data')
temp.flush()
# do something interesting with temp before it is destroyed