Python tempfile - Temp Files and Directories

by James Johnson

Programs often need temporary files and directories to store data. Sometimes they are created, used, and deleted immediately. Other times temporary files and directories are simply used as a way of determining a writable location that won’t clutter the file system. The Python tempfile module can be used as a reliable cross-platform solution to temporary files and directories.

tempfile Directory Location

The tempfile module determines the main temporary directory by looking for a writeable directory indicated by:

  1. TMP, TEMP, or TMPDIR environment variables
  2. Operating system specific paths:
    • RISC OS: Wimp$ScrapDir environment variable
    • Windows: C:\temp, C:\tmp, \temp, or \tmp
    • others: /tmp, /var/tmp, /usr/tmp
  3. current working directory

Python Temporary Directory Example

Temporary directories can be created using the tempfile.mkdtemp() function:

import tempfile
import shutil

tmpdir = tempfile.mkdtemp()
print("temporary directory at " + tmpdir)
# do things with the tmpdir
shutil.rmtree(tmpdir)
python example.py example1

Which outputs:

jelly$> python example.py example1
temporary directory at /tmp/tmpbvctltny

If you are using python 3, temporary directories can also be created using the tempfile.TemporaryDirectory() class. The created TemporaryDirectory instance can be used as a context manager, deleting the created temporary directory when the with block exits:

import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
    # do things with the tmpdir
    print("tmpdir location: {}".format(tmpdir))

print("tmpdir exists: {}".format(os.path.exists(tmpdir)))
python example.py example2

Which outputs:

jelly$> python3 example.py example2
tmpdir location: /tmp/tmpphc73m17
tmpdir exists: False

Python Temporary File Example

tempfile.TemporaryFile

Temporary files can be created by using tempfile.TemporaryFile:

import tempfile

with tempfile.TemporaryFile() as f:
    f.write(b"hello")
    f.seek(0)
    print("file name: {}".format(f.name))
    print("file contents: {}".format(f.read()))
python example.py example3

Which outputs:

jelly$> python example.py example3
file name: <fdopen>
file contents: hello

When the with block exits, the created temporary file will be deleted. Also note that objects returned from tempfile.TemporaryFile do not have a valid path on the filesystem.

tempfile.NamedTemporaryFile

If you require a temporary file with a valid filesystem path, use tempfile.NamedTemporaryFile instead:

import tempfile

with tempfile.NamedTemporaryFile() as f:
    f.write(b"hello")
    f.seek(0)
    print("file name: {}".format(f.name))
    print("file contents: {}".format(f.read()))
python example.py example4

Which outputs:

jelly$> python example.py example4
file name: /tmp/tmpZAVHNV
file contents: hello

tempfile.mkstemp

In the cases of both tempfile.TemporaryFile and tempfile.NamedTemporaryFile, the returned file object will be deleted when the file is closed. If you need a safe way to create a temporary file and not have it delete when it is closed, use tempfile.mkstemp:

import os
import tempfile

fd,tmpfile = tempfile.mkstemp()
print("fd: {}, tmpfile: {}".format(fd, tmpfile))
file_ = os.fdopen(fd, "w+b")
file_.write("hello there")
file_.seek(0)
print("tmpfile contents: {}".format(file_.read()))
file_.close()
print("tmpfile still exists: {}".format(os.path.exists(tmpfile)))
os.unlink(tmpfile)
python example.py example5

Which outputs:

jelly$> python example.py example5
fd: 3, tmpfile: /tmp/tmplouw6b
tmpfile contents: hello there
tmpfile still exists: True