While making a wsgi app I am in need of joining route names like /apple, apple/buy etc.
import os
path = os.path.join("/apple/", "/buy")
print("route path:", path)
Now the problem is it works fine in *nix systems but on dos ones the ouput could become:
route path: apple\buy
cuz what is wanted was unix style paths like - apple/buy
is their a way that I can always get unix style paths? should I use something other than os module?
CodePudding user response:
os.path
is a alias to posixpath
or ntpath
. You can import one of those modules directly to get the path rules you want. So, import posixpath
and use functions like posixpath.join
.
>>> import os
>>> os.path.__name__
'posixpath'
>>> import posixpath
>>> posixpath.join is os.path.join
True
>>>
You could
import os
import posixpath
os.path = posixpath
But that can be a bad choice because it would also affect any code that wants to work with local platform paths. Its usually best to use posixpath
directly as needed.
CodePudding user response:
os.path
is for path operations on the current operating system.
Use urllib.parse.urljoin
to join URL paths.