Skip to content

Commit

Permalink
forgot file
Browse files Browse the repository at this point in the history
  • Loading branch information
rdiankov committed Jan 6, 2012
1 parent 33a19b4 commit 7335bee
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions myrelpath.py
@@ -0,0 +1,19 @@
"""relpath is not present in python 2.5 and below, so hold an implementation of it.
"""
try:
from os.path import relpath
except ImportError:
from posixpath import curdir, sep, pardir, join, abspath, commonprefix

def relpath(path, start=curdir):
"""Return a relative version of a path"""
if not path:
raise ValueError("no path specified")
start_list = abspath(start).split(sep)
path_list = abspath(path).split(sep)
# Work out how much of the filepath is shared by start and path.
i = len(commonprefix([start_list, path_list]))
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return curdir
return join(*rel_list)

0 comments on commit 7335bee

Please sign in to comment.