Search code examples
pythonurlpathnormalize

How can I normalize/collapse paths or URLs in Python in OS independent way?


I tried to use os.normpath in order to convert http://example.com/a/b/c/../ to http://example.com/a/b/ but it doesn't work on Windows because it does convert the slash to backslash.


Solution

  • Here is how to do it

    >>> import urlparse
    >>> urlparse.urljoin("ftp://domain.com/a/b/c/d/", "../..")
    'ftp://domain.com/a/b/'
    >>> urlparse.urljoin("ftp://domain.com/a/b/c/d/e.txt", "../..")
    'ftp://domain.com/a/b/'    
    

    Remember that urljoin consider a path/directory all until the last / - after this is the filename, if any.

    Also, do not add a leading / to the second parameter, otherwise you will not get the expected result.

    os.path module is platform dependent but for file paths using only slashes but not-URLs you could use posixpath,normpath.