Python TemporaryDirectory
Create private scratch entries with distinct names.
A simple example
TemporaryDirectory
- Start with A temporary workspace
- Do this Create two independent temporary items
- Result Distinct items
Syntax
TemporaryDirectory(dir=base)
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyIsolate two simultaneous label previews
Two till operators can preview labels with the same product name at the same time. Ask the system to create two independent scratch entries and verify the names differ before writing any shared data.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
base = Path(folder)
with TemporaryDirectory(dir=base) as a, TemporaryDirectory(dir=base) as b:
if a != b and Path(a).is_dir() and Path(b).is_dir():
print("distinct")
distinct
The filesystem creates separate entries for the two jobs.
Locate a rehearsal score
The choir stores each score inside a season folder. Two students use distinct temporary workspaces with the same filename. Does a file saved by the first student appear in the second student’s workspace?
Locate a workshop handout
An instructor stores the current template in a class folder. Two students use distinct temporary workspaces with the same filename. Does a file saved by the first student appear in the second student’s workspace?
What to watch for
Keep the handle or path returned by the creation API and clean up after the job.