multipath module

This module implements functions for working with multiple directories at once.

A common use-case is when a project needs to build paths relative to multiple root directories and return the first path that exists. This module supplies the resolve method for doing that.

multipath.join(dirs, *paths)

Returns the first dir in dirs joined with *paths using os.path.join.

Parameters:
  • dirs – A list of dir strings
  • paths – Path components to join onto the first dir in dirs
Returns:

A path created by calling os.path.join on the first dir in dirs with *paths.

Raises:

ValueError: if dirs is empty

multipath.join_all(dirs, *paths)

Returns a list of paths created by calling os.path.join on each dir in dirs with *paths.

Parameters:
  • dirs – A list of dir strings
  • path – Path components to join onto each dir in dirs
Returns:

A list of paths created by calling os.path.join on each dir in dirs with *paths

multipath.resolve(dirs, *paths)

Joins paths onto each dir in dirs using os.path.join until one of the join results is found to exist and returns the existent result.

Parameters:
  • dirs – A list of dir strings to resolve against
  • paths – Path components to join onto each dir in dirs

:return A path created by calling os.path.join on a dir in dirs with *paths. :raises ValueError: If dirs is empty :raises FileNotFoundError: If joining paths onto all dirs in dirs always resulted in non-existent paths.

multipath.resolve_all(dirs, *paths)

Returns a list of paths created by joining paths onto each dir in dirs using os.path.join and discarding all join results that do not exist.

Parameters:
  • dirs – A list of dir strings to resolve against
  • paths – Path components to join onto each dir in dirs with os.path.join
Returns:

A list of paths created by joining paths onto each dir in dirs using os.path.join and discarding all

join results that do not exist.

multipath.try_join(dirs, *paths)

Returns the first dir in dirs joined with *paths using os.path.join, or None if dirs is empty.

Parameters:
  • dirs – A list of dir strings
  • paths – Path components to join onto the first dir in dirs
Returns:

A path created by calling os.path.join on the first dir in dirs with *paths, or None if dirs is

empty.

multipath.try_resolve(dirs, *paths)

Joins paths onto each dir in dirs using os.path.join until one of the join results is found to exist and returns the existent result. If none of the results exist, returns None.

Parameters:
  • dirs – A list of dir strings to resolve against
  • paths – Path components to join onto each dir in dirs

:return A path created by calling os.path.join on a dir in dirs with *paths, or None if joining paths onto all dirs in dirs always resulted in a non-existent path