On Wed, Jan 12, 2005 at 03:11:15AM -0800, Jeremy Huddleston wrote:
> Ok, I'm sure someone has some code for this lying around, so I really
> don't want to reinvent the wheel when I've got so much else to do...
> so...
>
> Does anyone know a way to find the relative path between two absolute
> paths... for instance, I want something which behaves like:
I have this code in Python, not heavily tested but I remember it seemed to
work once.
--
Tom
import os
import string
def relative(target, source):
thead, ttail = os.path.split(target[0].path)
shead, stail = os.path.split(source[0].path)
tparts = string.split(thead, os.sep)
sparts = string.split(shead, os.sep)
i = 1
try:
while tparts[i] == sparts[i]:
i += 1
except IndexError:
pass
ups = []
for j in range(i, len(tparts)):
ups.append('..')
return os.sep.join(ups + sparts[i:] + [stail])
--
[email protected] mailing list