PyDocs

Nav

File and Directory Access

PurePath.relative_to

pathlib

method of PurePath (source) Deprecated 3.12
  • PurePath.relative_to(other, walk_up=False)
Deprecated since 3.12, removed in 3.14. Passing additional positional arguments is deprecated; if supplied, they are joined with other.
Return the relative path to another path identified by the passed
arguments.  If the operation is not possible (because this is not
related to the other path), raise ValueError.

The *walk_up* parameter controls whether `..` may be used to resolve
the path.
Changed in 3.12
  • 3.12: The walk_up parameter was added (old behavior is the same as walk_up=False).
1 Example
>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')
PurePosixPath('etc/passwd')
>>> p.relative_to('/etc')
PurePosixPath('passwd')
>>> p.relative_to('/usr')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pathlib.py", line 941, in relative_to
    raise ValueError(error_message.format(str(self), str(formatted)))
ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other is absolute.
from the Python docs
See Also
No see-alsos for pathlib/PurePath.relative_to
0 Notes
No notes for PurePath.relative_to