cdd

pyhelpers.dirs.cdd(*subdir, mkdir=False, data_dir='data', **kwargs)[source]

Specify and resolve the pathname of a directory (or file) under the designated data_dir.

Parameters:
  • subdir (str | bytes | pathlib.Path | os.PathLike | None) – Name of a directory or directories (and/or a filename).

  • mkdir (bool) – Whether to create the directory if it does not exist. Defaults to False.

  • data_dir (str | bytes | pathlib.Path | os.PathLike) – Name of the directory where data is stored. Defaults to "data".

  • kwargs – Optional parameters passed to cd().

Returns:

Pathname of a directory or file under data_dir.

Return type:

pathlib.Path | str

Examples:

>>> from pyhelpers.dirs import cd, cdd, delete_dir
>>> from pathlib import Path

>>> cur_dir = cd()
>>> cur_dir == Path.cwd()
True

>>> test_path = cdd()
>>> # As `mkdir=False`, `test_path` will NOT be created if it doesn't exist
>>> test_path.relative_to(cur_dir)
WindowsPath('data')

>>> test_path = cdd(data_dir="test_cdd", mkdir=True)
>>> # As `mkdir=True`, `test_path` will be created if it doesn't exist
>>> test_path.is_dir()
True
>>> test_path.relative_to(cur_dir)
WindowsPath('test_cdd')

>>> # Delete the "test_cdd" folder
>>> delete_dir(test_path, verbose=True)
To delete the directory "./test_cdd/"
? [No]|Yes: yes
Deleting "./test_cdd/" ... Done.

>>> # Set `data_dir` to be `"tests"`
>>> test_path = cdd("data", data_dir="test_cdd", mkdir=True)
>>> test_path.relative_to(cur_dir)
WindowsPath('test_cdd/data')

>>> # Delete the "test_cdd" folder and the sub-folder "data"
>>> test_cdd = test_path.parent
>>> delete_dir(test_cdd, verbose=True)
To delete the directory "./test_cdd/" (Not empty)
? [No]|Yes: yes
Deleting "./test_cdd/" ... Done.
>>> # # Alternatively,
>>> # import shutil
>>> # shutil.rmtree(test_cdd)