ccd

pyhelpers.dirs.ccd(*subdir, **kwargs)[source]

Get the full pathname of a directory (or file), given an altered working directory.

Parameters:
  • subdir (str | os.PathLike[str] | bytes | os.Path[bytes]) – name of a directory

  • kwargs – [optional] parameters of the function cd()

Returns:

full pathname of a directory or that of a file (in an altered working directory)

Return type:

str

Examples:

>>> from pyhelpers.dirs import ccd
>>> import os

>>> init_cwd = os.getcwd()
>>> os.path.relpath(init_cwd)
'.'

>>> # Change the current working directory to "/new_cwd"
>>> new_cwd = "tests/new_cwd"
>>> os.makedirs(new_cwd, exist_ok=True)
>>> os.chdir(new_cwd)

>>> # Get the full path to a folder named "tests"
>>> path_to_tests = ccd("tests")
>>> os.path.relpath(path_to_tests)
'tests'

>>> path_to_tests_ = ccd("test1", "test2")
>>> path_to_tests_ == os.path.join(os.getcwd(), "test1", "test2")
True

>>> os.chdir(init_cwd)
>>> os.rmdir(new_cwd)