python-3.xpathlib

pathlib's relative_to doesn't accept walk_up parameter


I'm using Python 3.10.12. I'm trying to walk back up a path to find a given folder and walk back down the tree again from that folder. Here's my code:

from pathlib import Path
p = Path(__file__).relative_to('some-parent-path-i-know-exists', walk_up=True)

And here is the error I'm getting:

TypeError: PurePath.relative_to() got an unexpected keyword argument 'walk_up'

According to pathlib's documentation, I should be able to use the walk_up parameter to allow for the pattern to be matched even if the Path doesn't start with that pattern. My IDE even suggests walk_up as an auto-complete option for the call to the relative_to() function. That tells me the parameter does indeed exist.

I tried declaring p as a PurePath, but that didn't help.

What am I doing wrong?


Solution

  • You linked the Python3.12.1 documentation and as it says for walk_up parameter:

    Changed in version 3.12: The walk_up parameter was added (old behavior is the same as walk_up=False).

    If you check the same documentation for Python3.10, then you will see the walk_up parameter is not supported in that Python version. (I have checked the implementation of PurePath for Python3.10 and it's really not supported based on the implementation as well)