validate_dir

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

Validate the pathname of a directory.

Parameters:
  • path_to_dir (str | os.PathLike[str] | bytes | 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[str] | bytes | 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 validate_dir
>>> import os
>>> import pathlib
>>> dat_dir = validate_dir()
>>> os.path.relpath(dat_dir)
'.'
>>> dat_dir = validate_dir("tests")
>>> os.path.relpath(dat_dir)
'tests'
>>> dat_dir = validate_dir(subdir="data")
>>> os.path.relpath(dat_dir)
'data'