tempfile creates temporary files and directories with automatic or manual cleanup
Answer: The tempfile module creates temporary files and directories on every supported platform.
High level vs low level
TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFile are high-level interfaces. They clean up automatically and work as context managers. mkstemp and mkdtemp are lower-level and need manual cleanup.
Names include a string of random characters. Constructors also take arguments that control location and name.
See also
Claims
- id: C1 claim: The module creates temporary files and directories. source: S1 evidence: "This module creates temporary files and directories."
- id: C2 claim: It works on all supported platforms. source: S2 evidence: "It works on all"
- id: C3 claim: TemporaryDirectory is a high-level interface with automatic cleanup. source: S2 evidence: ":class:
TemporaryDirectory, and"
- id: C4 claim: mkstemp and mkdtemp require manual cleanup. source: S2 evidence: ":func:
mkstempand :func:mkdtempare lower-level"
Sources
- [S1] tempfile — Generate temporary files and directories. Python docs. https://docs.python.org/3/library/tempfile.html . License: PSF-2.0. Accessed 2026-11-16.
- [S2] tempfile.rst (CPython docs). python/cpython (GitHub). https://github.com/python/cpython/blob/main/Doc/library/tempfile.rst . License: PSF-2.0. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, tempfile — Generate temporary files and directories, PSF-2.0 (https://docs.python.org/3/license.html); python/cpython (GitHub), tempfile.rst (CPython docs), PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, tempfile — Generate temporary files and directories, PSF-2.0 (https://docs.python.org/3/license.html); python/cpython (GitHub), tempfile.rst (CPython docs), PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE).
Cite this
iinventedlight. 2026. "tempfile creates temporary files and directories with automatic or manual cleanup." iinventedlight Library, iil:note:e08aq5z3, version of 2026-11-16. https://iinventedlight.com/library/notes/python-tempfile/. License: CC-BY-4.0.
@misc{iil_note_e08aq5z3,
author = {{iinventedlight}},
title = {{tempfile creates temporary files and directories with automatic or manual cleanup}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/python-tempfile/},
note = {Permanent ID iil:note:e08aq5z3. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-tempfile.md.