Absolute to relative URL?

Massimo Valentini sixtysix at inwind.it
Thu Apr 4 10:52:11 EST 2002


"Max M" wrote
: A function that generates a relative path given two absolute urls?
:
: Not that I am aware of, but this problem was more fun than what I really
: should be working on today :-/, so here is an attempt:
:
: regards Max M
:
: -----------------------------------------------------
:
: from urlparse import urlparse, urljoin
:
:
: def relativePath(url1,url2):
:      "Returns relative path from url1 to url1"
...
:
:      diffStart = 0 # from where do the paths differ?
:      for i in range(min(u1Len, u2Len)):
:          if u1Parts[i] == u2Parts[i]:
:              diffStart += 1
...
: url1 = "http://foo/a/b/c"
: url2 = "http://foo/a/b/c"
: print "Excpect: ''"
: print repr(relativePath(url1,url2))
: print ""
...

I would suggest to consider even a case like this:

url1 = "http://foo/a/b/images"
url2 = "http://foo/a/b/c/d/images"
print "Expect: '../../../images'"
print repr(relativePath(url1,url2))
print ""


Massimo





More information about the Python-list mailing list