Python temporary working directory copy
If an external program needs a subdirectory to create and load multiple files, Python tempfile.TemporaryDirectory() creates a temporary working directory. shutil.copytree is used to recursively copy 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)