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.Path[bytes] | None) – pathname of a data directory, defaults to None

  • subdir (str | os.PathLike[str] | bytes | os.Path[bytes]) – name of a subdirectory to be examined if directory=None, defaults to ""

  • msg (str) – error message if data_dir is not a full pathname, defaults to "Invalid input!"

  • kwargs – [optional] parameters of 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'