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.
import tempfile
with tempfile.TemporaryFile(mode="w+") as tmp:
tmp.write("Temporary content")
tmp.seek(0)
print(tmp.read()) # Temporary contentTemporaryFile() 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.
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:
Line 0
Line 1
Line 2The 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.
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:
File path: /tmp/report_abc123.csv
File deleted after the with blocksuffix=".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:
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.
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:
Contents of temp directory:
/tmp/tmpabc123/input.txt
/tmp/tmpabc123/outputTemporaryDirectory() 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.
import tempfile
with tempfile.SpooledTemporaryFile(max_size=1024, mode="w+") as tmp:
tmp.write("Small data")
tmp.seek(0)
print(tmp.read()) # Small datamax_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
| Function | Has a path? | Auto clean? | Best for |
|---|---|---|---|
| TemporaryFile | No | Yes, on close | In-memory or anonymous temp data |
| NamedTemporaryFile | Yes | Yes, on close (delete=True) | Passing a path to another tool |
| SpooledTemporaryFile | No | Yes, on close | Mixed small/large data |
| TemporaryDirectory | Yes | Yes, on context exit | Scratch workspace with multiple files |
Practical example: safe file replacement
A common use for temporary files is safely updating a file without risking data loss.
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:
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
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=Falseonly when you need the file to persist and handle deletion yourself. - Temporary files are stored in the system temp directory by default.
Frequently Asked Questions
Where are temporary files stored?
When are temporary files deleted?
Can other programs read my temporary files?
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.
More in this topic
Format Dates and Times in Python
Learn how to use strftime and strptime to format dates as strings and parse date strings into Python datetime objects.
What Is the Python Standard Library?
Learn what the Python standard library is, why it matters, and how to use its built-in modules without installing anything extra.
Encode and Decode Base64 Data in Python
Learn how to use Python's base64 module to encode binary data as text and decode Base64 strings back to bytes.