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