bpod_core.misc.prune_empty_parent_directories

bpod_core.misc.prune_empty_parent_directories(target_directory, root_directory, *, remove_root=False) None

Remove empty parent directories recursively up to root directory.

Recursively removes the given directory if empty, then checks and removes parent directories up to (and optionally including) the root directory. Stops at the first non-empty directory encountered.

Parameters:
  • target_directory (PathLike or str) – Directory to check and remove if empty.

  • root_directory (PathLike or str) – Root directory to stop at. Must be a parent directory of target_directory.

  • remove_root (bool, optional) – If True, also remove root_directory if it becomes empty.

Raises:
  • ValueError – If target_directory is not a subpath of root_directory.

  • FileNotFoundError – If target_directory or root_directory does not exist.

  • NotADirectoryError – If target_directory or root_directory is not a directory.

Return type:

None