Last Updated: Sunday 26th May 2013

While programming in Python, there will likely be times where 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.

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.

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:

We get the following output:

And to check if the file handle has been closed:

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 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.

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • Diliupg

    simple but excellent tutorial.