cdd

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

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

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

  • data_dir (str | os.PathLike[str] | bytes | os.Path[bytes]) – name of a directory where data is (or will be) stored, defaults to "data"

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

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

Return path:

full pathname of a directory (or a file) under data_dir

Return type:

str

Examples:

>>> from pyhelpers.dirs import cdd, delete_dir
>>> import os

>>> path_to_dat_dir = cdd()
>>> # As `mkdir=False`, `path_to_dat_dir` will NOT be created if it doesn't exist
>>> os.path.relpath(path_to_dat_dir)
'data'

>>> path_to_dat_dir = cdd(data_dir="test_cdd", mkdir=True)
>>> # As `mkdir=True`, `path_to_dat_dir` will be created if it doesn't exist
>>> os.path.relpath(path_to_dat_dir)
'test_cdd'

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

>>> # Set `data_dir` to be `"tests"`
>>> path_to_dat_dir = cdd("data", data_dir="test_cdd", mkdir=True)
>>> os.path.relpath(path_to_dat_dir)
'test_cdd\data'

>>> # Delete the "test_cdd" folder and the sub-folder "data"
>>> test_cdd = os.path.dirname(path_to_dat_dir)
>>> delete_dir(test_cdd, verbose=True)
The directory "test_cdd\" is not empty.
Confirmed to delete it
? [No]|Yes: yes
Deleting "test_cdd\" ... Done.

>>> # # Alternatively,
>>> # import shutil
>>> # shutil.rmtree(test_cdd)