Python Glossary Русский

all Python 3tempfilefunction

tempfile.mkdtemp()

directories and paths

Creates a temporary directory with a unique name and returns its path; you are responsible for removing it.

Syntax

path = tempfile.mkdtemp(suffix=None, prefix=None, dir=None)

Unlike TemporaryDirectory, the directory is never removed for you: without shutil.rmtree() it piles up in /tmp run after run. It is created readable only by its owner (mode 0o700). Since Python 3.12 the path is always absolute even if dir is relative; earlier versions returned a relative one.

Examples

import os, shutil, tempfile
path = tempfile.mkdtemp(prefix='job_')
print(os.path.basename(path).startswith('job_'))  # → True
shutil.rmtree(path)  # сам не удалится
print(os.path.exists(path))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy