How to calculate the relative path between two directories?

I have 2 directories:

subdir1 = live/events/livepkgr/events/_definst_/ subdir2 = live/streams/livepkgr/streams/_definst_/ 

the result should be:

 diff_subdir = ../../../../streams/livepkgr/streams/_definst_/ 
+8
python
source share
2 answers

http://docs.python.org/library/os.path.html

os.path.relpath (path [, start]) Returns the relative path to the path to the path either from the current directory or from an optional starting point.

start by default os.curdir.

Availability: Windows, Unix.

New in version 2.6.

+16
source share
 >>> subdir1 = "live/events/livepkgr/events/_definst_/" >>> subdir2 = "live/streams/livepkgr/streams/_definst_/" >>> import os >>> os.path.relpath(subdir2, subdir1) '../../../../streams/livepkgr/streams/_definst_' >>> 
+16
source share

All Articles