Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving a relative url path to its absolute path

Tags:

python

url

path

Is there a library in python that works like this?

>>> resolvePath("http://www.asite.com/folder/currentpage.html", "anotherpage.html") 'http://www.asite.com/folder/anotherpage.html' >>> resolvePath("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") 'http://www.asite.com/folder/folder2/anotherpage.html' >>> resolvePath("http://www.asite.com/folder/currentpage.html", "/folder3/anotherpage.html") 'http://www.asite.com/folder3/anotherpage.html' >>> resolvePath("http://www.asite.com/folder/currentpage.html", "../finalpage.html") 'http://www.asite.com/finalpage.html' 
like image 229
Eric Palakovich Carr Avatar asked Jan 24 '09 19:01

Eric Palakovich Carr


People also ask

How do you convert relative path to absolute path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

What is absolute URL relative URL?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.

Is absolute URL better than relative?

An absolute URL contains more information than a relative URL does. Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.


1 Answers

Yes, there is urlparse.urljoin, or urllib.parse.urljoin for Python 3.

>>> try: from urlparse import urljoin # Python2 ... except ImportError: from urllib.parse import urljoin # Python3 ... >>> urljoin("http://www.asite.com/folder/currentpage.html", "anotherpage.html") 'http://www.asite.com/folder/anotherpage.html' >>> urljoin("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") 'http://www.asite.com/folder/folder2/anotherpage.html' >>> urljoin("http://www.asite.com/folder/currentpage.html", "/folder3/anotherpage.html") 'http://www.asite.com/folder3/anotherpage.html' >>> urljoin("http://www.asite.com/folder/currentpage.html", "../finalpage.html") 'http://www.asite.com/finalpage.html' 

for copy-and-paste:

try:     from urlparse import urljoin  # Python2 except ImportError:     from urllib.parse import urljoin  # Python3 
like image 185
James Brady Avatar answered Oct 25 '22 03:10

James Brady