On this page
  1. tempfile.TemporaryDirectory
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

tempfile.TemporaryDirectory

Prev: tempfile.SpooledTemporaryFile | Up: tempfile | Next: tempfile.TemporaryFile
Rel: tempfile.gettempdir, tempfile.gettempdirb, tempfile.gettempprefix, tempfile.gettempprefixb
Ref: docs.python.org — tempfile.TemporaryDirectory
Tags: #public #python3 #stdlib #tempfile


Import

import tempfile
from tempfile import TemporaryDirectory

Signature & Overview

tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False, *, delete=True)

Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example:

Methods & Attributes

Usage

from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory(prefix="demo_") as tmp:
    p = Path(tmp) / "data.txt"
    p.write_text("ok", encoding="utf-8")
    assert p.exists()
assert not Path(tmp).exists()

Gotchas

REPL Inspect

from tempfile import TemporaryDirectory
help(TemporaryDirectory)

Linked from

Related notes