Using pathlib's relative_to for directories on the same level
<p>The python library <a href="https://docs.python.org/3/library/pathlib.html" rel="noreferrer"><code>pathlib</code></a> provides <a href="https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.relative_to" rel="noreferrer"><code>Path.relative_to</code></a>. This function works fine if one path is a subpath of the other one, like this:</p>

<pre><code>from pathlib import Path
foo = Path("C:\\foo")
bar = Path("C:\\foo\\bar")
bar.relative_to(foo)

&gt; WindowsPath('bar')
</code></pre>

<p>However, if two paths are on the <em>same level</em>, <code>relative_to</code> does not work.</p>

<pre><code>baz = Path("C:\\baz")
foo.relative_to(baz)

&gt; ValueError: 'C:\\foo' does not start with 'C:\\baz'
</code></pre>

<p>I would expect the result to be</p>

<pre><code>WindowsPath("..\\baz")
</code></pre>

<p>The function <a href="https://docs.python.org/3.5/library/os.path.html#os.path.relpath" rel="noreferrer"><code>os.path.relpath</code></a> does this correctly:</p>

<pre><code>import os
foo = "C:\\foo"
bar = "C:\\bar"
os.path.relpath(foo, bar)

&gt; '..\\foo'
</code></pre>

<p>Is there a way to achieve the functionality of <code>os.path.relpath</code> using <code>pathlib.Path</code>?</p>