[Django]-Is it okay to use os.sep instead of "/" in url

3👍

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.)

👤Kos

4👍

os.sep will return \ on Windows – whether that’s what you want depends on the protocol you’re using I think, but broadly speaking using I think os.sep isn’t appropriate for URLs that aren’t using file:// (and even then it’s questionable).

You might find urlparse useful: https://docs.python.org/2/library/urlparse.html

3👍

From documentation:

The character used by the operating system to separate pathname
components. This is ‘/’ for POSIX and ‘\’ for Windows. Note that
knowing this is not sufficient to be able to parse or concatenate
pathnames — use os.path.split() and os.path.join() — but it is
occasionally useful. Also available via os.path.

So No, it is note safe to use.

For URI parsing, splitting, joining, etc, you should use the urllib.parse library. (called urlparse in python 2)

Leave a comment