Does os.path relpath produce an incorrect relative path?

MRAB python at mrabarnett.plus.com
Thu May 25 12:52:01 EDT 2023


On 2023-05-25 16:53, Eryk Sun wrote:
> On 5/25/23, BlindAnagram <BlindAnagram at 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

Well, 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.


More information about the Python-list mailing list