os.sep
gives you the separator for your current system's file system paths. Your system paths and URI paths aren't the same.
RFC 3986 gives:
A path consists of a sequence of path segments separated by a slash
("/") character.
If you have an URI like http://foo.bar.baz/a/b/c/d
, you should use urlsplit
to split it into components and extract the path
part. Then you can safely use .split('/')
to get the individual parts of this path, or use '/'.join
to construct a path from the segments (if you know that each segment is a valid segment according to the grammar).
The grammar doesn't permit this /
to be anything other than a separator in the path segment, check the RFC to be doubly sure. This doesn't hold for the whole URL though, /
will mean different things in other URL sections.
The opposite of urlsplit
is urlunsplit
which can do what you want once you have the path assembled.
To be safe, you should percent-encode the individual path parts before joining them with /
using urllib.quote('/test', '')
(mind the second parameter - /
isn't escaped here by default.)