Question :
When multiple directories need to be concatenated, as in an executable search path, there is an os-dependent separator character. For Windows it’s ';'
, for Linux it’s ':'
. Is there a way in Python to get which character to split on?
In the discussions to this question How do I find out my python path using python? , it is suggested that os.sep
will do it. That answer is wrong, since it is the separator for components of a directory or filename and equates to '\'
or '/'
.
Answer #1:
Answer #2:
It is os.pathsep
Answer #3:
Making it a little more explicit (For python newbies like me)
import os
print(os.pathsep)
Answer #4:
OK, so there are:
os.pathsep
that is;
and which is a separator in thePATH
environment variable;os.path.sep
that is/
in Unix/Linux andin Windows, which is a separator between path components.
The similarity is a source of confusion.
Answer #5:
This is a sample path for your working directory/specific folder –
import os
my = os.path.sep+ "testImages" + os.path.sep + "imageHidden.png"
print(my)
Output for Linux-
/home/*******/Desktop/folder/PlayWithPy/src/testImages/imageHidden.png
Output for Windows-
C:\Users\Administrator\Desktop\folder\tests\testImages\imageHidden.png