utils.py 557 B

123456789101112131415161718192021222324
  1. from pathlib import Path
  2. path_type_labels = {
  3. 'is_dir': 'directory',
  4. 'is_file': 'file',
  5. 'is_mount': 'mount point',
  6. 'is_symlink': 'symlink',
  7. 'is_block_device': 'block device',
  8. 'is_char_device': 'char device',
  9. 'is_fifo': 'FIFO',
  10. '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 getattr(p, method)():
  19. return name
  20. return 'unknown'