Safety Warning (Dry Runs and Backups)
flowchart TD
A["build the list of planned actions"] --> B["print every one"]
B --> C{"--apply passed?"}
C -->|no, the default| D["stop here -- nothing was changed"]
C -->|yes| E{"is there a backup?"}
E -->|no| F["make one first"]
E -->|yes| G["perform the actions"]
F --> G
G --> H["log what was actually done, not what was planned"]
D --> I["read the output, then re-run with --apply"]
Why this matters
Section titled “Why this matters”Automation can:
- delete thousands of files instantly
- overwrite important documents
- send emails/messages to the wrong people
You want guardrails.
Dry run pattern
Section titled “Dry run pattern”A dry run prints what would happen without doing it.
from pathlib import Path
def delete_tmp_files(folder: Path, dry_run: bool = True):
for path in folder.rglob("*.tmp"):
if dry_run:
print("[DRY RUN] would delete", path)
else:
print("deleting", path)
path.unlink()
delete_tmp_files(Path("./demo"), dry_run=True)Use a sandbox folder
Section titled “Use a sandbox folder”- Copy a small sample dataset
- Run scripts there first
Always keep backups
Section titled “Always keep backups”At minimum:
- zip the folder before modifying it
- store backups outside the working directory
Add logging (not print)
Section titled “Add logging (not print)”import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s"
)
logging.info("starting job")Add confirmations for destructive actions
Section titled “Add confirmations for destructive actions”from pathlib import Path
folder = Path("./demo")
ans = input(f"Really delete all .tmp files under {folder}? (yes/no) ")
if ans.strip().lower() != "yes":
raise SystemExit("Cancelled")pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading