Temporary Files & Directories
Published by Brayan K
The tempfile module creates files and directories in a safe, unique, platform-correct location for short-lived scratch data — and the high-level helpers clean themselves up automatically.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A scratch file has both a position and a lifetime
Temporary storage is useful when an operation needs a real file but its data should not become a permanent project artifact. Start by deciding who owns the cleanup and whether another operation needs a filename. Do not invent a predictable scratch filename in a shared directory and hope nobody else uses it. tempfile creates the resource as part of choosing its name.
from tempfile import TemporaryFile
with TemporaryFile(mode="w+t", encoding="utf-8") as scratch:
scratch.write("draft\nready")
print(repr(scratch.read()))
scratch.seek(0)
print(scratch.read().splitlines())
print(scratch.closed)
# ✅ Expected output:
# ''
# ['draft', 'ready']
# TrueWriting advances the file position. The first read starts at the end, so it finds no remaining text. seek(0) returns to the beginning; it does not clear the file. Leaving with closes the handle and performs the temporary-file cleanup. The handle can still be referenced afterward, but it is no longer usable for I/O.
from tempfile import TemporaryFile
with TemporaryFile(mode="w+t", encoding="utf-8") as scratch:
scratch.write("draft")
scratch.read()
# ✅ Expected error:
# ValueError: I/O operation on closed file.Do not rely on TemporaryFile.name as a reusable pathname: its visibility and representation depend on the platform. When a separate API genuinely needs a name, choose a named file or a directory you own. Basic file modes and ordinary file operations belong to File Handling.
Choose bytes or text before writing
from tempfile import TemporaryFile
with TemporaryFile() as scratch:
scratch.write(b"\x00\x01report")
scratch.seek(0)
data = scratch.read()
print(type(data).__name__)
print(data.hex())
# ✅ Expected output:
# bytes
# 00017265706f7274from tempfile import TemporaryFile
with TemporaryFile() as scratch:
scratch.write("report")
# ✅ Expected error:
# TypeError: a bytes-like object is required, not 'str'TemporaryFile defaults to a binary read/write mode. Use bytes there, or explicitly request a text mode and encoding as in the first example. A filename suffix does not select an encoding or transform bytes into text. Strings, Bytes & Unicode Encodings owns encoding and decoding; this lesson chooses the file interface that matches the data.
Put related files in one owned temporary directory
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
root = Path(directory)
report = root / "report.txt"
report.write_text("ready", encoding="utf-8")
print(report.read_text(encoding="utf-8"))
print(root.exists())
print(root.exists())
# ✅ Expected output:
# ready
# True
# FalseTemporaryDirectory removes its contents and directory when the context exits. A Path object is only a reference to a location; keeping it after exit does not keep the files alive. Finish reading or copy a deliberately chosen result to permanent storage before cleanup. Close handles first, especially when running on Windows.
from pathlib import Path
from tempfile import TemporaryDirectory
try:
with TemporaryDirectory() as directory:
root = Path(directory)
(root / "partial.txt").write_text("partial", encoding="utf-8")
raise ValueError("conversion rejected")
except ValueError as error:
print(error)
print(root.exists())
# ✅ Expected output:
# conversion rejected
# FalseThis is ordinary exception unwinding, not a guarantee against a killed process, machine shutdown or every filesystem error. Do not use temporary storage as the only durable copy of a result. If cleanup itself fails, investigate the handle or permission problem instead of routinely hiding it. Context-manager error flow is covered in contextlib: contextmanager, suppress, ExitStack.
Close a named file before reopening it portably
from pathlib import Path
from tempfile import NamedTemporaryFile
name = None
try:
with NamedTemporaryFile(mode="w", encoding="utf-8",
suffix=".txt", delete=False) as scratch:
name = Path(scratch.name)
scratch.write("shared with another API")
print(name.read_text(encoding="utf-8"))
finally:
if name is not None:
name.unlink()
print(name.exists())
# ✅ Expected output:
# shared with another API
# FalseThe with block closes the writer before the separate read. delete=False keeps the file available, so the outer finally block removes it. Cleanup is established before writing begins, and the name starts as None so a creation failure does not trigger removal of an unrelated path. Both writing and reopening are covered by the same ownership obligation.
Reopening an already-open named temporary file has different constraints across operating systems. Closing first and owning deletion works across Python 3.11–3.14. Newer keyword options are deliberately not needed here. When several files are involved, TemporaryDirectory is often simpler because one context owns all of them.
Spooling changes storage without changing the file interface
from tempfile import SpooledTemporaryFile
with SpooledTemporaryFile(max_size=16, mode="w+b") as scratch:
scratch.write(b"small")
scratch.seek(0)
before = scratch.read()
scratch.rollover()
scratch.seek(0)
after = scratch.read()
print(before == after)
print(after.decode("ascii"))
# ✅ Expected output:
# True
# smallSpooledTemporaryFile starts with an in-memory buffer and rolls to an operating-system file when its configured nonzero size threshold is exceeded. rollover forces that transition. Calling fileno also forces it because a real file descriptor is needed. Avoid inspecting private attributes to decide how to use the file; the public read/write/seek interface remains the same.
The default max_size is zero, which does not impose an automatic size-triggered rollover. Choose a positive threshold when memory use matters. Spooling is not compression, and the threshold does not promise a strict upper bound on all memory used by your program.
Low-level creation also gives you low-level responsibilities
import os
from pathlib import Path
from tempfile import TemporaryDirectory, mkstemp
with TemporaryDirectory() as directory:
descriptor, filename = mkstemp(dir=directory, suffix=".txt")
created = Path(filename)
try:
with os.fdopen(descriptor, "w", encoding="utf-8") as stream:
descriptor = None
stream.write("owned")
print(created.read_text(encoding="utf-8"))
finally:
if descriptor is not None:
os.close(descriptor)
created.unlink()
print(created.exists())
# ✅ Expected output:
# owned
# Falsemkstemp returns an open descriptor plus a name. fdopen transfers descriptor ownership to a file object; the example clears its own descriptor variable only after fdopen succeeds. If wrapping fails, the finally block still closes the original descriptor. Do not call os.close again after the file object has taken ownership.
Prefer the high-level context managers unless an API requires this control. The obsolete mktemp pattern only chooses a name, leaving a race before creation; it is not a substitute for mkstemp. The example bounds its work to a temporary directory it created and never prints machine-specific paths or descriptor numbers.
Complete a text-file round trip
Choose text mode, rewind, and read the result while the scratch file is open. Fill one gap for each operation using the literal hints.
from tempfile import TemporaryFile
with TemporaryFile(mode=___, encoding="utf-8") as scratch:
scratch.write("alpha\nbeta")
scratch.seek(___)
print(scratch.___().splitlines())
# 👉 replace ___ with "w+t"
# 👉 replace ___ with 0
# 👉 replace ___ with read
# ✅ Expected output:
# ['alpha', 'beta']Hand a named report to a reader without leaving it behind
Write a helper that creates a named UTF-8 temporary file with delete=False, writes three names, closes it, then reads and returns the number of lines through its pathname. Remove the file even if writing or reading raises. Print the count and whether the file was removed. Keep the path out of the output.
from pathlib import Path
from tempfile import NamedTemporaryFile
def count_report(names):
report = None
try:
with NamedTemporaryFile(mode="w", encoding="utf-8",
delete=False) as stream:
report = Path(stream.name)
stream.write("\n".join(names))
count = len(report.read_text(encoding="utf-8").splitlines())
finally:
if report is not None:
report.unlink()
return count, not report.exists()
count, removed = count_report(["Ada", "Lin", "Sam"])
print(count)
print(removed)
# ✅ Expected output:
# 3
# True
Practice quiz
What does this program print? from tempfile import TemporaryFile with TemporaryFile(mode="w+t") as f: f.write("abc") print(repr(f.read()))
- ''
- 'abc'
- None
- 3
Answer: ''. The write leaves the position at the end; read does not rewind automatically.
What does this program print? from tempfile import TemporaryFile with TemporaryFile() as f: print(type(f.read()).__name__)
- str
- bytes
- NoneType
- bytearray
Answer: bytes. The default mode is binary, so read returns bytes.
You keep a Path from inside TemporaryDirectory after the with block. What keeps its files alive?
- The Path reference
- The variable name
- Nothing; context exit removes the directory
- The .txt suffix
Answer: Nothing; context exit removes the directory. A Path object does not own the filesystem resource lifetime.
What does this program print? from tempfile import TemporaryFile with TemporaryFile(mode="w+t") as f: f.write("abc") f.seek(0) print(f.read(2))
- abc
- bc
- 2
- ab
Answer: ab. After rewinding, a two-character read consumes the first two characters.
Which reopening plan works across the supported Python versions and operating systems?
- Use delete=False, close the writer, reopen, and remove in finally
- Assume a default NamedTemporaryFile can always be reopened while open
- Use TemporaryFile.name as a portable filename
- Choose a name with mktemp and create it much later
Answer: Use delete=False, close the writer, reopen, and remove in finally. The plan separates the open-handle lifetime from the explicitly owned pathname lifetime.
What does this program print? from tempfile import TemporaryFile with TemporaryFile(mode="w+t") as f: f.write("x") print(f.closed)
- False
- True
- None
- x
Answer: True. Context exit closes the handle.
After os.fdopen succeeds, who should normally close that descriptor?
- Both the wrapper and a separate os.close call
- Nobody
- The owning file object
- The Path object
Answer: The owning file object. The wrapper owns the descriptor; closing it twice risks closing an unrelated reused descriptor.
What does SpooledTemporaryFile(max_size=0) mean?
- It always stores zero bytes
- It rolls over after the first byte
- It disables read and write
- There is no automatic size-threshold rollover
Answer: There is no automatic size-threshold rollover. Explicit rollover and fileno can still move the buffer to a real file.
Continue this course
- Previous: contextlib: contextmanager, suppress, ExitStack
- Next: Finding Files with glob & fnmatch — Match file paths with wildcards using glob and fnmatch
- Quick reference: Python cheat sheet