cd

pyhelpers.dirs.cd(*subdir, mkdir=False, cwd=None, back_check=False, **kwargs)[source]

Get the full pathname of a directory (or file).

Parameters:
  • subdir (str | os.PathLike[str] | bytes | os.Path[bytes]) – name of a directory or names of directories (and/or a filename)

  • mkdir (bool) – whether to create a directory, defaults to False

  • cwd (str | os.PathLike[str] | bytes | os.Path[bytes] | None) – current working directory, defaults to None

  • back_check (bool) – whether to check if a parent directory exists, defaults to False

  • kwargs – [optional] parameters (e.g. mode=0o777) of os.makedirs

Returns:

full pathname of a directory or that of a file

Return type:

str

Examples:

>>> from pyhelpers.dirs import cd
>>> import os
>>> import pathlib

>>> current_wd = cd()  # Current working directory
>>> os.path.relpath(current_wd)
'.'

>>> # The directory will be created if it does not exist
>>> path_to_tests_dir = cd("tests")
>>> os.path.relpath(path_to_tests_dir)
'tests'

>>> path_to_tests_dir = cd(pathlib.Path("tests"))
>>> os.path.relpath(path_to_tests_dir)
'tests'