Using Python’s tempfile module

Recently at work, I had to write a tool that processes files uploaded by our tutorial writers. I needed to build something that could read multiple files, manipulate the data in the files and produce output files based on the results of manipulating the data. Each of the output files produced during the script’s execution was no longer required after the script was done running. This created a problem in that many output files were created and this cluttered the file system with unnecessary files that would require deleting every time the script ran.

The tempfile module is very useful in situations like this. Tempfile allows you to create temporary files and directories that you don’t have to find and delete when your script is done with them.

 from tempfile import TemporaryFile
 
 
 # Create a temporary file and write some data to it
 fp = TemporaryFile('w+t')
 fp.write("Hello universe!")
 # Go back to the beginning and read data from file
 fp.seek(0)
 data = fp.read()
 # Close the file, it will be removed
 fp.close()
 

Alternatively, you can use a context manager to close the file automatically.

with TemporaryFile('w+t') as fp:
    fp.write("Hello universe!")
    fp.seek(0)
    fp.read()
# File is now closed and removed    

tempfile can also be used to make temporary directories:

import tempfile


with tempfile.TemporaryDirectory() as tmpdir:
    print("Created temporary directory ", tmpdir)

# Directory contents have been removed