vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj'
src_path = 'C:\\lib\\src\\'
rel_path = '..\\..\\..\\lib\\src'
[snip]
The first of these three results produces an incorrect relative path
because relpath does not strip off any non-directory tails before
comparing paths.
The first of these three results produces an incorrect relative path
because relpath does not strip off any non-directory tails before
comparing paths.
On 5/25/23, BlindAnagram <BlindAnagram@nowhere.org> wrote:
vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj'
src_path = 'C:\\lib\\src\\'
rel_path = '..\\..\\..\\lib\\src'
[snip]
The first of these three results produces an incorrect relative path
because relpath does not strip off any non-directory tails before
comparing paths.
The start path is assumed to be a directory, which defaults to the
current working directory, and the input paths are first resolved as
absolute paths. In order to reach src_path from vcx_path, one has to
traverse 3 levels up to the root directory.
https://docs.python.org/3/library/os.path.html#os.path.relpath
On 2023-05-25 16:53, Eryk Sun wrote:
On 5/25/23, BlindAnagram <BlindA...@nowhere.org> wrote:
vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj'
src_path = 'C:\\lib\\src\\'
rel_path = '..\\..\\..\\lib\\src'
[snip]
The first of these three results produces an incorrect relative path
because relpath does not strip off any non-directory tails before
comparing paths.
The start path is assumed to be a directory, which defaults to the
current working directory, and the input paths are first resolved as absolute paths. In order to reach src_path from vcx_path, one has to traverse 3 levels up to the root directory.
https://docs.python.org/3/library/os.path.html#os.path.relpathWell, it's not necessarily the optimal relative path, because it's not always necessary to go all the way up to the root, as in the first example.
Surprisingly (for me at least) the alternative provided by the pathlib
module 'relative_to' method doesn't provide for full relative path computation. I was expecting this would offer everything that os.path
offers but it doesn't in this case.
On 5/26/23, cactus <riemannic@gmail.com> wrote:
Surprisingly (for me at least) the alternative provided by the pathlib
module 'relative_to' method doesn't provide for full relative path
computation. I was expecting this would offer everything that os.path
offers but it doesn't in this case.
Starting with Python 3.12, the relative_to() method has a walk_up
parameter. It defaults to False for the sake of backward
compatibility.
https://docs.python.org/3.12/library/pathlib.html#pathlib.PurePath.relative_to
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 77:37:15 |
Calls: | 6,716 |
Files: | 12,247 |
Messages: | 5,357,739 |