While programming in Python, there will likely be times when you have some data that needs to be utilized or manipulated in the form of a file but hasn’t yet been written to one. Naturally, the first solution that comes to mind is to open a new or existing file, write the data and finally save it (if you’re unfamiliar with how to do this take a look at the article Reading and Writing Files in Python). However, it might also be the case that once your script(s) are finished running, you don’t need or want the file(s) anymore, and therefore, don’t want it hanging around in your or anyone else’s file system.

This is where the tempfile module comes in handy, providing functions that create temporary files which you don’t have to go and manually delete when your script exits. Let’s look at a couple simple examples to illustrate the basic way to use tempfile.

Creating a temporary file with tempfile is just like creating a regular one with the built-in open() method, except that no name need be given for a temporary file. We’ll open one, write some data to it, and then read back what we wrote.

# Importing only what we'll use
from tempfile import TemporaryFile
t = TemporaryFile()
data = 'A simple string of text.'

Now we have our temporary file t and our data string to write to it—we didn’t specify the mode for the file so it defaulted to ‘w+b’, meaning we can read and write to it with any kind of data. With the next three lines, we’ll write our data to the file and then read back what we wrote.

Python 3.x

>>> t.write(bytes(data, 'UTF-8'))
>>> # Makes sure the marker is at the start of the file
>>> t.seek(0)
A simple string of text.

Python 2.x

>>> t.write(data)
>>> # Makes sure the marker is at the start of the file
>>> t.seek(0)
A simple string of text.

Just as with any other file object we can write data to and read data from our temporary one. Again, the difference and the advantage is that once a TemporaryFile object is closed there isn’t a trace of it. This is especially advantageous when utilizing the with statement, which automatically does the simple cleanup of closing the file for you when the statement is complete:

Python 3.x

with TemporaryFile() as tempf:
data = (data + '\n') * 3
# Write the string thrice
tempf.write(bytes(data, 'UTF-8'))
tempf.seek(0)
print(tempf.read().decode())

Python 2.x

with TemporaryFile() as tempf:
# Write the string thrice
tempf.write((data + '\n') * 3)
tempf.seek(0)
print(tempf.read())

We get the following output:

A simple string of text.
A simple string of text.
A simple string of text.

And to check if the file handle has been closed:

>>> # Test to see if the file has been closed
>>> print(tempf.closed)
True

That is pretty much the gist of the temporary file. It’s as simple and easy to use as any other file object.

The tempfile module also provides a NamedTemporaryFile() method that provides a temporary file that will always have a definite and visible name in the file system (up until the time it’s closed of course). The name of the file can be accessed through its name attribute (if our file t were a NamedTemporaryFile we’d use t.name to access that info). Additionally, this type of temporary file gives the option of actually saving the file when it’s closed instead of deleting it.

If you need to create a temporary directory, this module also provides the mkdtemp() method to do so; however, unlike with TemporaryFile and NamedTemporaryFile objects, temporary directories  are not deleted without you manually deleting them—the only thing temporary about them is that they are by default stored in the temporary folder defined by the value of tempfile.tempdir. Still, the tools to create temporary files and directories provided by tempfile can be very useful, so don’t hesitate to give them a try.