Skip to content

Instantly share code, notes, and snippets.

@dmtucker
Created October 27, 2019 17:33
Show Gist options
  • Save dmtucker/f3c32611c2aba44c16e4264e63922013 to your computer and use it in GitHub Desktop.
Save dmtucker/f3c32611c2aba44c16e4264e63922013 to your computer and use it in GitHub Desktop.
import os
import py
def _relpath(path, *, relto):
# TODO py35+ commonpath = os.path.commonpath([path, relto])
commonpath = str(py.path.local(path).common(py.path.local(relto)))
def _commonpath_to(_path):
return _path[len(commonpath):].strip(os.path.sep)
relto_to_commonpath = os.path.sep.join(
os.path.pardir
for component in _commonpath_to(relto).split(os.path.sep)
if component
)
return os.path.join(relto_to_commonpath, _commonpath_to(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment