Sep-30-2019, 04:19 AM
i was trying to eliminate some messy code that was making some relative symlinks by using PurePath.relative_to() to create the relative path. but it is failing with ValueError. the documentation says this exception happens when the relative path is impossible. it gives an example of this exception which my messy code would have created a valid relative path. that example does:
it looks like this method is only intend for "inline" relative paths, not "common parent" relative paths. it can't even do "inline reverse" relative paths:
is the a real relative path function/method around somewhere?
p = PurePosixPath('/etc/passwd') p.relative_to('/usr')it describes this as a failure case. a valid relative path is
'../etc/passwd'
but they don't get it. here is test output i get:Output:>>> import pathlib
>>> p = pathlib.PurePosixPath('/etc/passwd')
>>> p.relative_to('/usr')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/pathlib.py", line 874, in relative_to
.format(str(self), str(formatted)))
ValueError: '/etc/passwd' does not start with '/usr'
>>>
it should give '../etc/passwd'
.it looks like this method is only intend for "inline" relative paths, not "common parent" relative paths. it can't even do "inline reverse" relative paths:
Output: >>> p.relative_to('/etc/foo/bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/pathlib.py", line 874, in relative_to
.format(str(self), str(formatted)))
ValueError: '/etc/passwd' does not start with '/etc/foo/bar'
>>>
it should give '../../passwd'
.is the a real relative path function/method around somewhere?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.