utils.py 572 B

123456789101112131415161718192021222324
  1. from pathlib import Path
  2. _PATH_TYPE_LABELS = {
  3. Path.is_dir: 'directory',
  4. Path.is_file: 'file',
  5. Path.is_mount: 'mount point',
  6. Path.is_symlink: 'symlink',
  7. Path.is_block_device: 'block device',
  8. Path.is_char_device: 'char device',
  9. Path.is_fifo: 'FIFO',
  10. Path.is_socket: 'socket',
  11. }
  12. def path_type_label(p: Path) -> str:
  13. """
  14. Find out what sort of thing a path is.
  15. """
  16. assert p.exists(), 'path does not exist'
  17. for method, name in _PATH_TYPE_LABELS.items():
  18. if method(p):
  19. return name
  20. return 'unknown'