check_relative_pathname

pyhelpers.dirs.check_relative_pathname(pathname, normalized=True)[source]

Checks if the pathname is relative to the current working directory.

This function returns a relative pathname of the input pathname to the current working directory if pathname is within the current working directory; otherwise, it returns a copy of the input.

Parameters:
  • pathname (str | bytes | pathlib.Path) – Pathname (of a file or directory).

  • normalized (bool) – Whether to normalize the returned pathname; defaults to True.

Returns:

A location relative to the current working directory if pathname is within the current working directory; otherwise, a copy of pathname.

Return type:

str

Tests:

>>> from pyhelpers._cache import _check_relative_pathname
>>> from pyhelpers.dirs import cd
>>> _check_relative_pathname(pathname=".")
'.'
>>> _check_relative_pathname(pathname=cd())
'.'
>>> _check_relative_pathname(pathname="C:/Windows")
'C:/Windows'
>>> _check_relative_pathname(pathname="C:\Program Files", normalized=False)
'C:\Program Files'