Convert absolute URLs to relative URLs in Python

Even when it is better to write links etc. with relative paths, such as when uploading from the development environment to the production server.

Is the function that finds the relative path from the file or directory that is the starting point like this?

import sys, os
from urlparse import urlparse, urlunparse

def relurl(absolute, origin):
    a = urlparse(absolute)
    o = urlparse(origin)

    #When the domain is different, it is difficult to think of a relative path, so return it as it is
    if a.netloc != o.netloc:
        return absolute

    relative = os.path.relpath(a.path, os.path.dirname(o.path))
    return urlunparse(('','',relative) + a[3:])

def main():
    params = sys.argv
    print relurl(params[1], params[2])

if __name__ == '__main__':
    main()

Recommended Posts

Convert absolute URLs to relative URLs in Python
Convert markdown to PDF in Python
Python> List> Convert relative paths to absolute paths> all_filepaths = [datas_path + fp for fp in train_filepaths]
Convert psd file to png in Python
Convert from Markdown to HTML in Python
Convert PDFs to images in bulk with Python
Convert exponential notation float to str in Python
Convert cubic mesh code to WKT in Python
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
To flush stdout in Python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert CIDR notation netmask to dotted decimal notation in Python
Login to website in Python
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert CIDR notation in Python
Speech to speech in python [text to speech]
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
Relative url handling in python
Convert python 3.x code to python 2.x
Post to Slack in Python
A story about how to specify a relative path in python.
Convert images passed to Jason Statham-like in Python to ASCII art
Convert Excel file to text in Python for diff purposes
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Try to calculate Trace in Python
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
An alternative to `pause` in Python
Determine the date and time format in Python and convert to Unixtime
[GCP] How to publish Cloud Storage signed URLs (temporary URLs) in Python
Convert files written in python etc. to pdf with syntax highlighting
Read big endian binary in Python and convert it to ndarray
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Python] How to convert db file to csv
[Introduction to Python] How to use class in Python?
Convert memo at once with Python 2to3
Install Pyaudio to play wave in python
How to access environment variables in Python
Generate AWS-S3 signed (time-limited) URLs in Python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
Convert Python> two value sequence to dictionary
How to dynamically define variables in Python