Creates an unnamed temporary file opened in binary or text mode.
On UNIX-like systems it typically has no name in the filesystem; on Windows it may appear but remains temporary.
The file is deleted automatically when closed or when the context block exits.
Ideal for internal scratch space that doesn’t need to be passed to external processes.
import tempfile
with tempfile.TemporaryFile(mode="w+t", encoding="utf-8") as temp_file:
temp_file.write("This is some temporary data.")
temp_file.seek(0)
print("Content from TemporaryFile:")
print(temp_file.read())
tempfile.NamedTemporaryFile()
Creates a temporary file with a visible name in the filesystem.
Default delete=True removes the file when closed; delete=False leaves it for manual cleanup.
Use when you need to pass a filename to another process or library.
Supports custom suffix, prefix, and dir parameters for naming and placement.
import tempfile
from pathlib import Path
# Auto-delete on with exit
path = None
with tempfile.NamedTemporaryFile(mode="w+t", encoding="utf-8", suffix=".log") as temp_file:
path = Path(temp_file.name)
print(f"Created temp file at {path}. Exists: {path.exists()}")
print(f"After close. Exists? {path.exists()}")
# Persist after with exit
path_persistent = None
with tempfile.NamedTemporaryFile(
mode="w+t",
encoding="utf-8",
suffix=".log",
delete=False
) as temp_file:
path_persistent = Path(temp_file.name)
print(f"Created temp file at {path}. Exists: {path.exists()}")
print(f"After close. Exists? {path_persistent.exists()}")
if path_persistent.exists():
path_persistent.unlink()
print(f"After unlink. Exists? {path_persistent.exists()}")
tempfile.TemporaryDirectory()
Creates a new temporary directory, returned as a path string.
When used in a with block, the directory and everything inside it are deleted on exit.
Ideal for workflows that produce multiple temporary files or subdirectories.
import tempfile
from pathlib import Path
temp_path = None
with tempfile.TemporaryDirectory(prefix="batch_job_") as temp_dir:
print(f"{temp_dir} - type: {type(temp_dir)}")
temp_path = Path(temp_dir)
(temp_path / "file1.txt").write_text("data")
subdir = temp_path / "subdir"
subdir.mkdir(exist_ok=True)
(subdir / "file2.txt").write_text("data2")
print(f"Contents: {[p.name for p in temp_path.iterdir()]}")
print(f"After close. Exists? {temp_path.exists()}")
Common Pitfalls & How to Avoid Them
Calling os.rmdir() or Path.rmdir() on a non-empty directory raises an error; use shutil.rmtree() for recursive deletion.
Forgetting to delete files created with delete=False in NamedTemporaryFile can leave orphaned files.
On Windows, other processes can’t open an open temporary file. Use delete=False and close it before sharing the name.
Relying on a temporary file’s name after closing a TemporaryFile is impossible, since it may never have had one.
python
FoldFold allExpandExpand allAre you sure you want to delete this link?Are you sure you want to delete this tag?
The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community