Using pathlib's relative_to for directories on the same level

JFB picture JFB · Jun 28, 2016 · Viewed 7.7k times · Source

The python library pathlib provides Path.relative_to. This function works fine if one path is a subpath of the other one, like this:

from pathlib import Path
foo = Path("C:\\foo")
bar = Path("C:\\foo\\bar")
bar.relative_to(foo)

> WindowsPath('bar')

However, if two paths are on the same level, relative_to does not work.

baz = Path("C:\\baz")
foo.relative_to(baz)

> ValueError: 'C:\\foo' does not start with 'C:\\baz'

I would expect the result to be

WindowsPath("..\\baz")

The function os.path.relpath does this correctly:

import os
foo = "C:\\foo"
bar = "C:\\bar"
os.path.relpath(foo, bar)

> '..\\foo'

Is there a way to achieve the functionality of os.path.relpath using pathlib.Path?

Answer

Carel picture Carel · Apr 25, 2017

The first section solves the OP's problem, though if like me, he really wanted the solution relative to a common root then the second section solves it for him. The third section describes how I originally approached it and is kept for interest sake.

Relative Paths

Recently, as in Python 3.4-6, the os.path module has been extended to accept pathlib.Path objects. In the following case however it does not return a Path object and one is forced to wrap the result.

foo = Path("C:\\foo")
baz = Path("C:\\baz")
Path(os.path.relpath(foo, baz))

> Path("..\\foo")

Common Path

My suspicion is that you're really looking a path relative to a common root. If that is the case the following, from EOL, is more useful

Path(os.path.commonpath([foo, baz]))

> Path('c:/root')

Common Prefix

Before I'd struck upon os.path.commonpath I'd used os.path.comonprefix.

foo = Path("C:\\foo")
baz = Path("C:\\baz")
baz.relative_to(os.path.commonprefix([baz,foo]))

> Path('baz')

But be forewarned you are not supposed to use it in this context (See commonprefix : Yes, that old chestnut)

foo = Path("C:\\route66\\foo")
baz = Path("C:\\route44\\baz")
baz.relative_to(os.path.commonprefix([baz,foo]))

> ...
> ValueError : `c:\\route44\baz` does not start with `C:\\route`

but rather the following one from J. F. Sebastian.

Path(*os.path.commonprefix([foo.parts, baz.parts]))

> Path('c:/root')

... or if you're feeling verbose ...

from itertools import takewhile
Path(*[set(i).pop() for i in (takewhile(lambda x : x[0]==x[1], zip(foo.parts, baz.parts)))])