From docs:
You can use monkey patching.
I guess it's similar to UNIX.
Quote:Note
Although os.path.relpath() and PurePath.relative_to() have some overlapping use-cases, their semantics differ enough to warrant not considering them equivalent.
You can use monkey patching.
from pathlib import Path from os.path import relpath Path.relpath = lambda self, start=None: Path(relpath(self, start)) # or define a function def rp(self, start=None): return Path(relpath(self, start)) # Path.relpath = rp my_path = Path.home() / '.local' / 'bin' rel = my_path.relpath('/var/log/nginx') print(my_path) print(rel)
Output:/home/deadeye/.local/bin
../../../home/deadeye/.local/bin
Windows:from pathlib import Path from os.path import relpath from os import environ Path.relpath = lambda self, start=None: Path(relpath(self, start)) # or define a function def rp(self, start=None): return Path(relpath(self, start)) # Path.relpath = rp # Windows has some special Paths stored in environment variables PROGRAMFILES = Path(environ['PROGRAMFILES']) LOCALAPPDATA = Path(environ['LOCALAPPDATA']) rel_prog_path = 'Testprogramm/data' curdir = PROGRAMFILES / rel_prog_path target = LOCALAPPDATA / rel_prog_path print(curdir) print(target) print(target.relpath(curdir))Maybe someone can try this on Mac. I don't have Mac.
I guess it's similar to UNIX.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!