Create Temporary Files and Directories in Python

Learn how to use Python's tempfile module to create temporary files and directories that are automatically cleaned up.

6 min read

The tempfile module in the Python standard library creates temporary files and directories that you can use for intermediate data, testing, or any situation where you need storage that should not outlive your program. The module handles naming, placement in the system's temp directory, and automatic cleanup.

pythonpython
import tempfile
 
with tempfile.TemporaryFile(mode="w+") as tmp:
    tmp.write("Temporary content")
    tmp.seek(0)
    print(tmp.read())  # Temporary content

TemporaryFile() creates an anonymous temporary file. The file is deleted as soon as it is closed. Using a with statement ensures it closes and cleans up even if an exception occurs.

Creating anonymous temporary files

TemporaryFile returns a file-like object with no visible name on the filesystem. Use it when you do not need a path, only a place to write and read data.

pythonpython
import tempfile
 
with tempfile.TemporaryFile(mode="w+") as tmp:
    for i in range(3):
        tmp.write(f"Line {i}\n")
    tmp.seek(0)
    for line in tmp:
        print(line.strip())

The loop reads the file back one line at a time, printing each of the three lines that were written earlier:

texttext
Line 0
Line 1
Line 2

The mode parameter works like open(): "w+" for reading and writing text, "w+b" for bytes. After writing, you must seek(0) to rewind to the beginning before reading.

By default, TemporaryFile opens in "w+b" mode (binary read-write). Pass mode="w+" explicitly for text mode.

Named temporary files

NamedTemporaryFile creates a file with a visible path on disk. Use it when you need to pass the file path to another program, library, or system command.

pythonpython
import tempfile
 
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", prefix="report_", delete=True) as tmp:
    tmp.write("name,score,grade\nMaya,92,A\n")
    tmp.flush()
    print(f"File path: {tmp.name}")
 
print("File deleted after the with block")

The path includes the random middle section between the prefix and suffix, and by the time the second print runs, the file has already been removed from disk:

texttext
File path: /tmp/report_abc123.csv
File deleted after the with block

suffix=".csv" and prefix="report_" control the filename. The middle part is randomly generated. delete=True (the default) removes the file when it is closed.

If you need the file to persist after the context manager exits, pass delete=False:

pythonpython
import tempfile
 
tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False)
tmp.write('{"status": "ok"}')
tmp.close()
 
print(f"File saved at: {tmp.name}")
 
import os
os.unlink(tmp.name)

With delete=False, you are responsible for removing the file. Use os.unlink(path), one of the file-management functions in the os module, when you are done.

Temporary directories

TemporaryDirectory creates a folder for scratch files. The folder and everything inside it is deleted when the context manager exits.

pythonpython
import tempfile
from pathlib import Path
 
with tempfile.TemporaryDirectory() as tmpdir:
    tmp = Path(tmpdir)
    (tmp / "input.txt").write_text("Hello from temp")
    (tmp / "output").mkdir()
    (tmp / "output" / "result.json").write_text('{"count": 42}')
 
    print("Contents of temp directory:")
    for entry in sorted(tmp.iterdir()):
        print(f"  {entry}")

The listing shows the file and the output folder created inside the temporary directory, both of which disappear as soon as the with block ends:

texttext
Contents of temp directory:
  /tmp/tmpabc123/input.txt
  /tmp/tmpabc123/output

TemporaryDirectory() returns the path as a string. Use pathlib.Path(tmpdir) to get a Path object for convenient file operations. When the with block ends, the directory and all its contents are removed.

This pattern is ideal for tests that need a scratch filesystem, build scripts that generate intermediate artifacts, or any situation where you need a disposable workspace.

SpooledTemporaryFile: memory until a size limit

SpooledTemporaryFile keeps data in memory until it exceeds a threshold, then spills to disk. This gives you the speed of in-memory I/O for small data and the capacity of disk for large data.

pythonpython
import tempfile
 
with tempfile.SpooledTemporaryFile(max_size=1024, mode="w+") as tmp:
    tmp.write("Small data")
    tmp.seek(0)
    print(tmp.read())  # Small data

max_size=1024 means data under 1 KB stays in memory. Larger writes automatically create a temporary file on disk. The interface is the same as TemporaryFile.

Use SpooledTemporaryFile when you are not sure how large the intermediate data will be and want to optimize for both small and large cases.

Choosing the right tempfile function

FunctionHas a path?Auto clean?Best for
TemporaryFileNoYes, on closeIn-memory or anonymous temp data
NamedTemporaryFileYesYes, on close (delete=True)Passing a path to another tool
SpooledTemporaryFileNoYes, on closeMixed small/large data
TemporaryDirectoryYesYes, on context exitScratch workspace with multiple files

Practical example: safe file replacement

A common use for temporary files is safely updating a file without risking data loss.

pythonpython
import tempfile
import os
from pathlib import Path
 
def safe_write(filepath, content):
    directory = Path(filepath).parent
    with tempfile.NamedTemporaryFile(mode="w", dir=directory, prefix=".tmp_", delete=False) as tmp:
        tmp.write(content)
        tmp_path = tmp.name
    os.replace(tmp_path, filepath)

Calling the function writes the new content to a temporary file first and only swaps it into place once the write succeeds, so a crash partway through never leaves config.json in a half-written state:

pythonpython
safe_write("config.json", '{"theme": "dark", "version": 2}\n')

The function writes to a temporary file in the same directory as the target, then atomically replaces the original with os.replace(). If the program crashes during the write, the original file is untouched. Only the temporary file is left behind.

Common mistakes

Forgetting to seek before reading. After writing to a TemporaryFile, the file position is at the end. Call tmp.seek(0) before reading, or you will get an empty result.

Using NamedTemporaryFile on Windows without close. On Windows, you cannot reopen a NamedTemporaryFile while it is still open. If you need another process to read the file, call .close() on the temp file first, or pass delete=False and manage cleanup yourself.

Not using a context manager. Without a with statement, you must call .close() on temporary files and .cleanup() on temporary directories manually. If an exception occurs before cleanup, the temp data stays on disk.

Assuming the system temp directory is cleaned automatically. The OS may clean /tmp on reboot, but do not rely on this. Always clean up your temporary files explicitly with a context manager or a try/finally block.

Rune AI

Rune AI

Key Insights

  • Use tempfile.TemporaryFile() for anonymous temporary files that are deleted on close.
  • Use tempfile.NamedTemporaryFile() when you need a path to pass to another tool.
  • Use tempfile.TemporaryDirectory() for a scratch folder with automatic cleanup.
  • Always use context managers (with) for guaranteed cleanup.
  • Use delete=False only when you need the file to persist and handle deletion yourself.
  • Temporary files are stored in the system temp directory by default.
RunePowered by Rune AI

Frequently Asked Questions

Where are temporary files stored?

By default, `tempfile` stores files in the system's standard temp directory. On macOS and Linux, this is usually `/tmp`. On Windows, it is typically `C:\Users\<user>\AppData\Local\Temp`. You can override this by passing the `dir` argument to any tempfile function.

When are temporary files deleted?

Files created with `TemporaryFile` and `NamedTemporaryFile` are deleted as soon as they are closed. Directories created with `TemporaryDirectory` are deleted when the context manager exits. For files opened with `delete=False`, you are responsible for deleting them.

Can other programs read my temporary files?

Yes, if they have filesystem permissions. Temporary files are regular files on disk. Use `os.umask` or file permissions if you need to restrict access. For sensitive data, consider encrypting the content before writing or using an in-memory buffer like `SpooledTemporaryFile` with a size limit.

Conclusion

The tempfile module handles temporary files and directories safely. Use TemporaryFile for anonymous temporary data, NamedTemporaryFile when you need a visible path on disk, and TemporaryDirectory when you need a scratch workspace. Always use context managers to guarantee cleanup, even when exceptions occur.