Combine relative path with absolute path in Python

---
date: Aug 12, 2017
tags:
- Python
language: English
---

Recently I need to combine a absolute link with a relative link, so I tried to hard code it but failed. After checking online, there is actually a lib in Python called urlparse that have a function to combine this kind of url. the use is also simple:

1
2
3
from urlparse import urljoin
urljoin('http://mysite.com/foo/bar/x.html', '../../images/img.png')
#'http://mysite.com/images/img.png'

Navigation


Creative Commons License

Unless specified, all the articles are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


It uses a self-hosted Open Web Analytics to get insights. Click here to optout.

The site is powered by Hexo. The source code of this website is here, so that you can see the spaghetti code behind my blog.


Main | Blog | Comic | Podcasts (Preparing) | RSS | Sitemap
2008 - 2022 | Patrick WU Jinming