[New-bugs-announce] [issue44078] Output relative path when using PurePath.relative_to

Mark Hammond report at bugs.python.org
Sat May 8 11:43:11 EDT 2021


New submission from Mark Hammond <hammond at finleme.com>:

Comparing two paths, PurePath.relative_to fails with ValueError if the second path is not in the first.

>>> Path('/a/b').relative_to('/a/b/c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/pathlib.py", line 928, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/a/b' is not in the subpath of '/a/b/c' OR one path is relative and the other is absolute.

Please extend PurePath.relative_to so it is able to output a relative path in form of '..' instead of an error. Currently, the only way to do this is to use relpath from os.path but it would be very useful if Path was able to output the relative path.

----------
components: Library (Lib)
messages: 393262
nosy: mhammondr
priority: normal
severity: normal
status: open
title: Output relative path when using PurePath.relative_to
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44078>
_______________________________________


More information about the New-bugs-announce mailing list