cd_data

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

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

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

  • data_dir (str | os.PathLike | bytes) – Name of the directory to store data; defaults to "data".

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

  • as_str (bool) – If True, forces the return type to be a standard string instead of a path object. Defaults to False.

  • kwargs – [Optional] Additional parameters (e.g. mode=0o777) for the method pathlib.Path.mkdir.

Returns:

A normalized pathlib.Path object (or str) of the requested target.

Return type:

pathlib.Path | str

Examples:

>>> from pyhelpers.dirs import cd_data
>>> from pathlib import Path
>>> test_path = cd_data("tests", mkdir=False)
>>> test_path.relative_to(Path.cwd())  # on Windows
WindowsPath('pyhelpers/data/tests')