What is the `tempfile` module for?
IntermediateConceptExceptions, context managers, files & logging
Answer
It creates temp files and directories securely (unique names, 0600 permissions, no race with predictable /tmp/foo paths).
import tempfile
with tempfile.TemporaryDirectory() as d:
subprocess.run(["git", "clone", "--depth=1", repo, d], check=True)
... # directory deleted on exit
Never build temp paths by hand like /tmp/deploy.sh; another user can pre-create or symlink that path.
More on Exceptions, context managers, files & logging
How does `try` / `except` / `else` / `finally` work?BeginnerWhy is a bare `except:` (or `except Exception: pass`) dangerous?BeginnerWhat is exception chaining, and what is `raise ... from`?IntermediateWhen should you define custom exception classes?IntermediateWhat is the EAFP vs LBYL style, and which does Python prefer?IntermediateHow does a `with` statement work, and why use it for files?Beginner