This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Output relative path when using PurePath.relative_to
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: pathlib's relative_to should behave like os.path.relpath
View: 40358
Assigned To: Nosy List: Socob, andrei.avk, mhammondr
Priority: normal Keywords:

Created on 2021-05-08 15:43 by mhammondr, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg393262 - (view) Author: Mark Hammond (mhammondr) Date: 2021-05-08 15:43
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.
msg393286 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-05-08 21:21
duplicate of https://bugs.python.org/issue40358 , which has an open patch.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88244
2022-02-22 23:52:20Socobsetnosy: + Socob
2021-05-14 22:17:49terry.reedysetstatus: open -> closed
superseder: pathlib's relative_to should behave like os.path.relpath
resolution: duplicate
stage: resolved
2021-05-08 21:21:05andrei.avksetnosy: + andrei.avk
messages: + msg393286
2021-05-08 15:43:11mhammondrcreate