Temporary Files and Directories in Python
Create secure scratch files and disposable workspaces with tempfile, choose an appropriate interface, and clean up reliably.
Intermediate
Temporary storage is useful when a program needs scratch data, generated files, or a short-lived workspace. Python’s
tempfile module creates temporary files and directories with generated names; its high-level interfaces can manage cleanup for you.
Choose the temporary resource for the job
Use
TemporaryDirectory() when several files belong to one disposable workspace,
TemporaryFile() for scratch data accessed through a file object,
NamedTemporaryFile() when another operation needs a visible pathname, and
SpooledTemporaryFile() when small data can stay in memory before rolling over to disk. Prefer these managed interfaces over generating a filename and creating it later.
The context-manager protocol still owns resource lifetime. The difference from
ExitStack is the job being solved:
tempfile creates temporary storage;
ExitStack coordinates cleanup for a variable set of resources.
Create a disposable workspace
A
TemporaryDirectory() context manager creates a private temporary directory and removes the directory and its contents when the block ends. Build child paths with
pathlib.Path rather than joining strings by hand.
Output
Ready to run.
Use a temporary directory when the output consists of multiple related files. The generated path is for the lifetime of that workspace; do not save it as if it were permanent application data.
Use TemporaryFile for scratch data
A
TemporaryFile() gives you a file-like object that is automatically removed when closed. Its default mode is binary (
w+b); choose a text mode and encoding explicitly when working with strings. Do not rely on the temporary file having a reusable visible pathname on every platform.
Output
Ready to run.
The file position advances as you write. Call
seek(0) before reading from the beginning. Exiting the block closes the object and lets the operating system remove the temporary storage.
Use NamedTemporaryFile when a path is needed
A
NamedTemporaryFile() guarantees a visible name available from its
name attribute. By default it is deleted when closed. If you must close it and reopen it by pathname in a portable workflow, use
delete=False, remember the path, and remove it in a
finally block. This avoids platform-specific assumptions about reopening a still-open temporary file.
Output
Ready to run.
The
delete=False setting transfers cleanup responsibility to your code. Always keep cleanup next to the operation that creates the file, and clean up even if reading or processing fails.
Output
Ready to run.
Pass
dir= to place a temporary file in a specific directory and use
prefix= or
suffix= to make its purpose recognizable. The generated part of the name is still created by the module; never guess the full name yourself.
Let SpooledTemporaryFile start in memory
A
SpooledTemporaryFile() keeps data in memory until its size exceeds
max_size; it also rolls over when
fileno() is called or when
rollover() is called. It then keeps the same file-like interface while using disk-backed storage. This can avoid disk I/O for small data without requiring your code to choose a separate implementation.
Output
Ready to run.
Use the public
rollover() operation when you need to force disk-backed storage. Avoid depending on private implementation details to inspect whether the spool has rolled over.
Use low-level creation only when needed
mkstemp() and
mkdtemp() securely create a temporary file or directory, but they leave more cleanup work to the caller.
mkstemp() returns an operating-system file descriptor and a pathname; close the descriptor and remove the file yourself. Never use the deprecated
mktemp(), which returns a name before creating the file and leaves a race window.
Output
Ready to run.
Choose the high-level interfaces unless you specifically need the descriptor or pathname control. If a temporary file must be created in a particular filesystem area, use the
dir= argument instead of changing
tempfile.tempdir globally.
Avoid predictable names and abandoned temporary files
Let
tempfile generate the name securely; do not build a name such as
"report.tmp" in a shared directory and assume it is free. Use
prefix=,
suffix=, and
dir= to control the name pattern and location without guessing the complete filename. Automatic cleanup is helpful, but it is not a substitute for handling abrupt termination, permission errors, or files that your own code deliberately keeps.
Official reference: Python 3.15 tempfile documentation
Quick check
Which interface is best when a calculation needs several temporary files in one workspace that should all disappear afterward?
from pathlib import Path
from tempfile import TemporaryDirectory
with () as directory_name:
output = Path(directory_name) / "result.txt"
output.write_text("done", encoding="utf-8")
print(output.read_text(encoding="utf-8"))
Quick check
A file-like object is enough for scratch text, and no other program needs its pathname. Which interface fits best?
from tempfile import TemporaryFile
with TemporaryFile(mode="w+t", encoding="utf-8") as scratch:
scratch.write("draft")
print(scratch.read())
Quick check
The program must close a temporary file and reopen it by name on different platforms. Which setting makes cleanup your responsibility?
from tempfile import NamedTemporaryFile
with NamedTemporaryFile(mode="w", encoding="utf-8", ) as temporary:
name = temporary.name
temporary.write("text")
Quick check
What can force a SpooledTemporaryFile to move its contents to disk even before max_size is exceeded?
from tempfile import SpooledTemporaryFile
with SpooledTemporaryFile(max_size=100, mode="w+t", encoding="utf-8") as scratch:
scratch.write("small data")
scratch.()
scratch.seek(0)
print(scratch.read())
Quick check
What must the caller do after tempfile.mkstemp() returns?
import os
from pathlib import Path
from tempfile import mkstemp
fd, name = mkstemp(text=True)
path = Path(name)
try:
finally:
path.unlink(missing_ok=True)
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.