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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Here is how to do it
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 useposixpath,normpath
.Neither
urljoin
norposixpath.normpath
do the job properly.urljoin
forces you to join with something, and doesn't handle absolute paths or excessive..
s correctly.posixpath.normpath
collapses multiple slashes and removes trailing slashes, both of which are things URLs shouldn't do.The following function resolves URLs completely, handling both
.
s and..
s, in a correct way according to RFC 3986.You can then call it on a complete URL as following.
For more information on the considerations that have to be made when resolving URLs, see a similar answer I wrote earlier on the subject.
adopted from os module " - os.path is one of the modules posixpath, or ntpath", in your case explicitly using posixpath.