Does os.path relpath produce an incorrect relative path?

Eryk Sun eryksun at gmail.com
Thu May 25 11:53:13 EDT 2023


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


More information about the Python-list mailing list