Python temporary working directory copy
When using Python with an external program that expects a subdirectory to create and load multiple files, it can be convenient to use tempfile.TemporaryDirectory() to create a temporary working directory and copy out all files if the call to the external program succeeds.
from pathlib import Path
import tempfile
import subprocess
import shutil
import uuid
file = Path(f"~/{uuid.uuid4().hex}.txt").expanduser()
# toy file
file.write_text("Hello World!")
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as f:
shutil.copy(file, f)
subprocess.check_call(["cat", Path(f) / file.name])
new_dir = file.parent / f"{file.stem}"
print(f"\n{file} Solved, copy to {new_dir}")
shutil.copytree(f, new_dir)