Python
temporary directory
tempfile module
file handling
programming tutorial

How do I create a temporary directory in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Temporary directories are the right tool when code needs short-lived disk space for downloads, extracted archives, test fixtures, or intermediate build output. In Python, the standard library already handles naming, uniqueness, and cleanup, so the main job is choosing the right API for the lifecycle you want.

Use TemporaryDirectory for Most Cases

The safest default is tempfile.TemporaryDirectory. It creates a new directory in the system temp area and deletes it automatically when the context exits.

python
1import tempfile
2from pathlib import Path
3
4with tempfile.TemporaryDirectory(prefix="report-") as tmp:
5    root = Path(tmp)
6    data_file = root / "output.txt"
7    data_file.write_text("generated data\n", encoding="utf-8")
8    print(data_file.read_text(encoding="utf-8").strip())
9
10print("temporary directory cleaned up")

This pattern is good because ownership is obvious. The directory exists only for the work inside the with block, and cleanup is tied to normal control flow rather than a later manual step.

It is also safer than inventing a path by concatenating strings. The tempfile module creates a unique directory name and avoids the race conditions that appear when two processes guess the same temp path.

Use mkdtemp When You Need Manual Lifetime Control

Sometimes the directory must survive past a single block of code. A long-running worker might create a temp workspace, pass its path to another function, and remove it only after multiple stages finish. In that case, use tempfile.mkdtemp and clean it yourself.

python
1import shutil
2import tempfile
3from pathlib import Path
4
5tmp_root = Path(tempfile.mkdtemp(prefix="job-"))
6print(f"created: {tmp_root}")
7
8try:
9    (tmp_root / "input").mkdir()
10    (tmp_root / "input" / "config.ini").write_text("[app]\nmode=test\n", encoding="utf-8")
11    print((tmp_root / "input" / "config.ini").read_text(encoding="utf-8"))
12finally:
13    shutil.rmtree(tmp_root, ignore_errors=True)
14    print("removed temp directory")

This is still a good pattern, but you now own cleanup. If you pick mkdtemp, pair it with try and finally so the directory is removed even when one processing step raises an exception.

Choose the Parent Directory Deliberately

By default, Python uses the operating system temp area, but you can choose a different parent directory. That matters when a workflow needs a fast local SSD, a writable container volume, or a predictable location for debugging.

python
1import tempfile
2from pathlib import Path
3
4base_dir = Path("/tmp")
5
6with tempfile.TemporaryDirectory(dir=base_dir, prefix="image-build-") as tmp:
7    root = Path(tmp)
8    (root / "artifact.bin").write_bytes(b"demo")
9    print(root)

If you are debugging a failing task, it can also be useful to keep the directory instead of deleting it immediately. A small flag makes that easy:

python
1import os
2import shutil
3import tempfile
4from pathlib import Path
5
6keep_temp = os.getenv("KEEP_TEMP_DIR") == "1"
7tmp_root = Path(tempfile.mkdtemp(prefix="debug-"))
8
9try:
10    (tmp_root / "log.txt").write_text("inspect me later\n", encoding="utf-8")
11    print(tmp_root)
12finally:
13    if not keep_temp:
14        shutil.rmtree(tmp_root, ignore_errors=True)

That gives you a clean default in production and an escape hatch when you need to inspect generated files by hand.

Temporary Directories Work Well with pathlib

The creation functions in tempfile return strings, but most file operations become clearer when you convert that path to a Path object. pathlib makes nested directories, file creation, and recursive iteration easier to read.

python
1import tempfile
2from pathlib import Path
3
4with tempfile.TemporaryDirectory() as tmp:
5    root = Path(tmp)
6    (root / "cache").mkdir()
7    (root / "cache" / "values.txt").write_text("1\n2\n3\n", encoding="utf-8")
8
9    for item in root.rglob("*"):
10        print(item.relative_to(root))

This is especially useful in tests, where you often need to create a small tree of files and assert on exact paths.

Common Pitfalls

The most common mistake is using mkdtemp and forgetting to remove the directory afterward. That leaves stale folders behind and slowly fills the temp volume.

Another mistake is hard-coding names like "/tmp/myapp" and assuming they are unique. Shared machines, CI runners, and concurrent test jobs make that assumption unsafe.

Developers also sometimes expect automatic cleanup to happen after a crash or forced termination. Context-managed cleanup works during normal teardown, but abrupt process termination can still leave temp directories on disk. For important systems, periodic cleanup of stale directories is still worth considering.

Finally, avoid mixing string concatenation with file paths when Path objects already solve the problem cleanly. Manual path building is easier to get wrong on different platforms.

Summary

  • Use tempfile.TemporaryDirectory() when the directory should disappear automatically.
  • Use tempfile.mkdtemp() only when you need the directory to outlive one block of code.
  • Pair manual temp directories with try and finally cleanup.
  • Convert the returned path to pathlib.Path for clearer file operations.
  • Do not handcraft temp names when the standard library already provides safe unique directories.

Course illustration
Course illustration

All Rights Reserved.