mattrguerra/transcription-pipelinemainPrivate on GitHub, published here in full

filing.py

Language Python797 B29 lines

transcription_pipeline/filing.py

  1. """Collision-safe names and moves: never overwrite, add ' (2)', ' (3)', ..."""
  2. from __future__ import annotations
  3. import shutil
  4. from pathlib import Path
  5. def unique_path(directory: Path, name: str) -> Path:
  6. directory.mkdir(parents=True, exist_ok=True)
  7. target = directory / name
  8. stem, suffix = Path(name).stem, Path(name).suffix
  9. n = 2
  10. while target.exists():
  11. target = directory / f"{stem} ({n}){suffix}"
  12. n += 1
  13. return target
  14. def write_unique(directory: Path, name: str, text: str) -> Path:
  15. target = unique_path(directory, name)
  16. target.write_text(text, encoding="utf-8")
  17. return target
  18. def move_unique(src: Path, directory: Path) -> Path:
  19. target = unique_path(directory, src.name)
  20. shutil.move(str(src), str(target))
  21. return target