fluentfs.paths.matches.matches_base_path

fluentfs.paths.matches.matches_base_path(path: str, base_paths: Union[str, List[str]]) bool

Check whether a path matches one of the given base paths.

A path matches a given base path if the maximally expanded version of the base path is a parent of the maximally expanded version of the path.

For example if path=”/home/username/somedir” and base_path=”/home/username” then this function returns True because “/home/username” is a parent of “/home/username/somedir”.

The maximal expansion of the paths are needed to make this function work with relative paths and/or paths that contain ~ and/or environment variables.

For example assume that ~ points to “/home/username” and there is a “somedir” directory inside “/home/username”. Then if you call this function with path=”somedir” and base_path=”~” it will return True since “somedir” will be maximally expanded to “/home/username/somedir” and “~” will be maximally expanded to “/home/username”.

More examples with absolute paths:

  • matches_base_path(“/home/username/somedir”, “/home/username”) returns True

  • matches_base_path(“/home/username/somedir”, “/home”) returns True

  • matches_base_path(“/home/username/somedir”, “/”) returns True

  • matches_base_path(“/home/username/somedir”, “/home/username/otherdir”) returns False

More examples with relative paths (assuming you are in the “/home/username” directory, there is a “/home/username/somedir” directory and ~ points to “/home/username”):

  • matches_base_path(“somedir”, “/home/username”) returns True

  • matches_base_path(“somedir”, “~”) returns True

  • matches_base_path(“.”, “~”) returns True

  • matches_base_path(“somedir”, “otherdir”) returns False

Parameters:
  • path – The given path.

  • base_paths – Either a single base path or a list of base paths.

Returns:

True, if the path matches one of the base paths, False otherwise.