Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Hands on the tempfile Module in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFile are high-level interfaces which provide automatic cleanup and can be used as context managers. mkstemp() and mkdtemp() are lower-level functions which require manual cleanup.

import os
import tempfile
from pathlib import Path

tempfile.TemporaryFile

fp = tempfile.TemporaryFile()
fp.write(b"how are you")
11
fp.seek(0)
0
fp.read()
b'how are you'
fp.close()
type(fp)
_io.BufferedRandom
fp.name
58
with tempfile.TemporaryFile() as fp:
    fp.write(b"how are you")
    fp.seek(0)
    print(fp.read())
b'how are you'

tempfile.TemporaryDirectory

Notice that after finish of the with block, the temporary directory is removed automatically.

with tempfile.TemporaryDirectory() as tempdir:
    print(tempdir)
/tmp/tmpwnml8m3t
type(tempdir)
str
Path(tempdir).exists()
False

tempfile.mkstemp

  1. The returned file descriptor must be closed (using os.close manually or using a contextual manager) to avoid leak of resource!

  2. The suffix option can be used to create a temporary file with a specific file extension.

Below are examples of using tempfile.mkstemp in correct ways. The first example close the file descriptor manually using os.close. The second example close the file automatically using the with block.

fd, file = tempfile.mkstemp()
print(file)
os.write(fd, b"how are you")
os.close(fd)
/tmp/tmp90pu002w
!cat {file}
how are you
fd, file = tempfile.mkstemp()
print(file)
with os.fdopen(fd, "w") as fout:
    fout.write("I am fine.")
/tmp/tmp4shh3pnx
!cat {file}
I am fine.

Create a temporary SQL file.

tempfile.mkstemp(suffix=".sql")
(57, '/tmp/tmpzjxy8tg9.sql')

tempfile.mkdtemp

import tempfile

tempfile.mkdtemp()
'/tmp/tmpwusvuq3c'

Set a Different Temporary Directory

In certain situations, you might want to set a different temporary directory than the default one. For example, if an application leverages temporary files/dirs extensively while there’s very limited space in the temporary directory (commonly seen when the application is running in a container environment on cloud), you can run into “OSError: No space left on the device”. An easy fix of this issue is to set environment variables to point the temporary directory to an existing directory with enough space which is writable by the user. The code below illustrate how to change the default temporary directory. Notice that tempfile.tempdir = None is added to clear possible caching. For more details, please refer to tempfile.gettempdir .

import tempfile
import os
from pathlib import Path

tempfile.tempdir = None
os.environ["TMPDIR"] = str(Path.home())
tempfile.mkstemp()
(57, '/Users/dclong/tmptci7lsir')