Create A Temporary File

Preliminaries

from tempfile import NamedTemporaryFile

Create A Temporary File

f = NamedTemporaryFile('w+t')

Write To The Temp File

# Write to the file, the output is the number of characters
f.write('Nobody lived on Deadweather but us and the pirates. It wasn’t hard to understand why.')
85

View The Tmp File's Name

f.name
'/var/folders/0b/pj3wsd750fjf8xzfb0n127w80000gn/T/tmphv1dkovx'

Read The File

# Go to the top of the file
f.seek(0)

# Read the file
f.read()
'Nobody lived on Deadweather but us and the pirates. It wasn’t hard to understand why.'

Close (And Thus Delete) The File

f.close()