There is a very useful class called os.path in python, but when combining absolute and relative paths,
# abs_path = "/usr/hoge/", rel_path = "./poyo/"
path.normpath(path.join(abs_path, rel_path))
# "/usr/hoge/poyo"
I often do it. I thought this could be used with url,
# abs_path = "http://hoge/", rel_path = "./poyo/"
path.normpath(path.join(abs_url, rel_url)
# "http:/hoge/poyo/"
I was angry that I couldn't open the url. That's why I was a little worried, but the reason was simple: the url "http: // hoge /" was converted to "http: / hoge" in path.normpath. Since path is a class for file system paths, it had this specification. It seems to use the urljoin method of the urlparse class to join urls.
# abs_path = "http://hoge/", rel_path = "./poyo/"
urlparse.urljoin(abs_url, rel_url)
# "http://hoge/poyo/"
Recommended Posts