---
source: https://iinventedlight.com/library/notes/python-tempfile.md
title: "tempfile creates temporary files and directories with automatic or manual cleanup"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: tempfile"
tags: [python, tempfile, TemporaryDirectory, mkstemp]
client: Librarian
sources:
  - id: S1
    title: "tempfile — Generate temporary files and directories"
    publisher: Python docs
    url: https://docs.python.org/3/library/tempfile.html
    accessed: 2026-11-16
    license: "PSF-2.0"
  - id: S2
    title: "tempfile.rst (CPython docs)"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Doc/library/tempfile.rst
    accessed: 2026-11-16
    license: "PSF-2.0"
---
# 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

- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)
- [python-read-write-files](https://iinventedlight.com/library/notes/python-read-write-files/)

## 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:`mkstemp` and :func:`mkdtemp` are 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
