summaryrefslogtreecommitdiffstats
path: root/Lib/posixpath.py
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-03-16 22:16:08 (GMT)
committerCollin Winter <collinw@gmail.com>2007-03-16 22:16:08 (GMT)
commit6f187743ffbce522c9d686ae5393272c1eb85dcc (patch)
tree49954823525566e80d435c3f9acff30a1a54e58d /Lib/posixpath.py
parent6de691d78ce696d897e4c58c3fc8fd7d3f6f1359 (diff)
downloadcpython-6f187743ffbce522c9d686ae5393272c1eb85dcc.zip
cpython-6f187743ffbce522c9d686ae5393272c1eb85dcc.tar.gz
cpython-6f187743ffbce522c9d686ae5393272c1eb85dcc.tar.bz2
Patch 1339796: add a relpath() function to os.path.
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r--Lib/posixpath.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 661d8db..9117208 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -21,7 +21,7 @@ __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
"ismount","walk","expanduser","expandvars","normpath","abspath",
"samefile","sameopenfile","samestat",
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
- "devnull","realpath","supports_unicode_filenames"]
+ "devnull","realpath","supports_unicode_filenames","relpath"]
# strings representing various path-related bits and pieces
curdir = '.'
@@ -382,3 +382,18 @@ def _resolve_link(path):
return path
supports_unicode_filenames = False
+
+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:]
+ return join(*rel_list)