resolve_dir

pyhelpers.dirs.resolve_dir(path_to_dir=None, subdir='', msg='Invalid input!', **kwargs)[source]

Resolves a directory path into an absolute pathname.

This function takes a variety of input types and ensures they are converted into a standardized absolute path string. It handles relative paths by anchoring them to a default directory and resolves missing inputs using the provided subdir.

Parameters:
  • path_to_dir (str | os.PathLike | bytes | None) – Pathname of a data directory; if path_to_dir=None (default), it examines subdir to construct a valid directory path.

  • subdir (str | os.PathLike | bytes) – Name of a subdirectory to be examined if path_to_dir=None; defaults to "".

  • msg (str) – Error message if path_to_dir is not a valid full pathname; defaults to "Invalid input!".

  • kwargs – [Optional] Additional parameters for the function pyhelpers.dirs.cd().

Returns:

Valid full pathname of a directory.

Return type:

str

Examples:

>>> from pyhelpers.dirs import resolve_dir
>>> import os
>>> import pathlib
>>> dat_dir = resolve_dir()
>>> os.path.relpath(dat_dir)
'.'
>>> dat_dir = resolve_dir("tests")
>>> os.path.relpath(dat_dir)
'tests'
>>> dat_dir = resolve_dir(subdir="data")
>>> os.path.relpath(dat_dir)
'data'