_path.py 749 B

1234567891011121314151617181920212223242526272829
  1. import os
  2. from typing import Union
  3. _Path = Union[str, os.PathLike]
  4. def ensure_directory(path):
  5. """Ensure that the parent directory of `path` exists"""
  6. dirname = os.path.dirname(path)
  7. os.makedirs(dirname, exist_ok=True)
  8. def same_path(p1: _Path, p2: _Path) -> bool:
  9. """Differs from os.path.samefile because it does not require paths to exist.
  10. Purely string based (no comparison between i-nodes).
  11. >>> same_path("a/b", "./a/b")
  12. True
  13. >>> same_path("a/b", "a/./b")
  14. True
  15. >>> same_path("a/b", "././a/b")
  16. True
  17. >>> same_path("a/b", "./a/b/c/..")
  18. True
  19. >>> same_path("a/b", "../a/b/c")
  20. False
  21. >>> same_path("a", "a/b")
  22. False
  23. """
  24. return os.path.normpath(p1) == os.path.normpath(p2)