_check_saving_path

pyhelpers.store._check_saving_path(path, verbose=False, msg_prefix='', state_verb='Saving', state_prep='to', msg_suffix='', end=' ... ', skip_updating_state=False, indent=None, msg_wrap_limit=None, return_info=False, **kwargs)[source]

Verifies a file path before saving, creates directories, and manages console output.

Parameters:
  • path (str | pathlib.Path | os.PathLike) – Destination file path.

  • verbose (bool | int) – Whether to print relevant information to the console; 2 enables CWD boundary warnings; defaults to False.

  • msg_prefix (str) – Text prefixed to the default print message; defaults to "".

  • state_verb (str) – Verb indicating the action being performed, e.g. saving or updating; defaults to "Saving".

  • state_prep (str) – Preposition associated with state_verb; defaults to "to".

  • msg_suffix (str) – Text suffixed to the default print message; defaults to "".

  • end (str) – String passed to the end parameter of print; defaults to " ... ".

  • skip_updating_state (bool) – If True, state_verb does not flip to "Updating"; defaults to False.

  • indent (int | str | None) – Indentation level; defaults to None.

  • msg_wrap_limit (int | None) – Threshold for multi-line wrapping; If the string exceeds this value (e.g. 100), it will be split at (before) state_prep to improve readability when being printed. If None (default), the printed string is in a single line.

  • return_info (bool) – Whether to return file path information; defaults to False.

Returns:

A tuple containing the absolute path, the relative directory path, and the extension.

Return type:

tuple

Tests:

>>> from pyhelpers.store import _check_saving_path
>>> from pyhelpers.dirs import cd
>>> path = cd()
>>> _check_saving_path(path, verbose=True)
Traceback (most recent call last):
  ...
    raise ValueError(f"The input '{path}' appears to be a directory, not a file path.")
ValueError: The input '<path>' appears to be a directory, not a file path.
>>> path = "pyhelpers.txt"
>>> _check_saving_path(path, verbose=True); print("Passed.")
Saving "pyhelpers.txt" ... Passed.
>>> path = cd("tests", "documents", "pyhelpers.txt")
>>> _check_saving_path(path, verbose=True); print("Passed.")
Saving "pyhelpers.txt" to "./tests/documents/" ... Passed.
>>> _check_saving_path(path, verbose=True, msg_wrap_limit=40); print("Passed.")
Saving "pyhelpers.txt" ...
  to "./tests/documents/" ... Passed.
>>> path = "C:\Windows\pyhelpers.txt"
>>> _check_saving_path(path, verbose=2); print("Passed.")
Saving "pyhelpers.txt" to "C:/Windows/" ... Passed.
  Warning: "C:/Windows/pyhelpers.txt" is outside the current working directory.
>>> path = "C:\pyhelpers.txt"
>>> _check_saving_path(path, verbose=2); print("Passed.")
Saving "pyhelpers.txt" to "C:/" ... Passed.
  Warning: "C:/pyhelpers.txt" is outside the current working directory.
>>> _check_saving_path(path, verbose=2, indent=4, msg_wrap_limit=30); print("Passed.")
    Saving "pyhelpers.txt" to "C:/" ... Passed.
      Warning: "C:/pyhelpers.txt" is outside the current working directory.