All articles
2 min read

How to Create Temporary Directories and Files in Python

Python's tempfile module creates temporary files and directories that clean up automatically. Learn TemporaryDirectory, NamedTemporaryFile, mkstemp, and when to use each.

Temporary files and directories are essential for tests, data processing pipelines, and scripts that generate intermediate output that shouldn't persist. Python's tempfile module handles all of this: it creates uniquely named resources in the system temp directory and cleans them up automatically when you're done.

Finding the Temp Directory

import tempfile
print(tempfile.gettempdir())
# /tmp  (Linux/macOS)
# C:\Users\username\AppData\Local\Temp  (Windows)

Temporary Directories

Using TemporaryDirectory

The safest approach — the directory and everything inside it is deleted automatically when the with block exits:

import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
    print(f"Temp dir: {tmpdir}")
    # Create files inside
    filepath = os.path.join(tmpdir, "output.csv")
    with open(filepath, "w") as f:
        f.write("col1,col2\n1,2\n")
    print(os.listdir(tmpdir))  # ['output.csv']

# tmpdir and its contents are deleted here
print(os.path.exists(tmpdir))  # False

Using tempfile.mkdtemp() (Manual Cleanup)

If you need the directory to outlive a with block, use mkdtemp(). You are responsible for deleting it:

import tempfile
import shutil

tmpdir = tempfile.mkdtemp(prefix="myapp_", suffix="_cache")
print(tmpdir)  # e.g. /tmp/myapp_x3k9q1_cache

try:
    # ... do work with tmpdir ...
    pass
finally:
    shutil.rmtree(tmpdir)  # always clean up

Temporary Files

Using NamedTemporaryFile

Creates a temp file with an accessible name on disk — useful when you need to pass the file path to another process:

import tempfile

with tempfile.NamedTemporaryFile(suffix=".json", delete=True) as f:
    f.write(b'{"key": "value"}')
    f.flush()
    print(f.name)  # e.g. /tmp/tmpq7x2k3.json
    # pass f.name to another process here

# file is deleted when the with block exits (delete=True by default)

Windows note: On Windows, NamedTemporaryFile cannot be opened by another process while it's still open. Set delete=False and manually delete it after you're done:

f = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
# ... use f.name ...
f.close()
os.unlink(f.name)

Using tempfile.mkstemp() (Low-Level)

Returns a file descriptor and path. Gives you full control but requires manual cleanup:

import tempfile
import os

fd, path = tempfile.mkstemp(suffix=".log")
try:
    with os.fdopen(fd, "w") as f:
        f.write("log entry\n")
    print(f"Written to {path}")
finally:
    os.unlink(path)  # delete manually

Choosing the Right Tool

| Need | Use | |---|---| | Temp dir, auto-cleanup | TemporaryDirectory (context manager) | | Temp dir, manual control | mkdtemp() + shutil.rmtree() | | Temp file with accessible path | NamedTemporaryFile | | Temp file, low-level control | mkstemp() + os.unlink() |

Conclusion

Default to TemporaryDirectory and NamedTemporaryFile — they handle cleanup automatically and are the least error-prone. Fall back to mkdtemp() or mkstemp() only when you need the resource to outlive its creating scope, and always clean up in a finally block.