from os import path from pathlib import Path def relative_path(*, to, ref): # pathlib.Path(x).relative_to(y) cannot handle y not being under x, # os.path.dirname('x') yields '' rather than '.'. # 😮‍💨 return path.relpath(to, Path(ref).parent)