183

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 '/'.

5 Answers 5

243

os.pathsep

The character conventionally used by the operating system to separate search path components (as in PATH), such as ':' for POSIX or ';' for Windows. Also available via os.path.

Sign up to request clarification or add additional context in comments.

4 Comments

If, like me, you didn't read the body of this question and just went by the title, you'll think this is the character that separates elements of a filesystem path (forward slash on Linux and MacOSX, backslash on Windows). It's not, it the character that separates elements of a shell PATH that is used to locate executable commands. os.sep or os.path.sep is what you need for filesystem paths.
os.pathsep is : or ; while os.path.sep is \\ or / - sometimes the names in Python strike me as shockingly poorly chosen. That . makes way too big of a difference.
@Perry i edited the question. hopefully my edit clears up the confusion (i indicated that this question is asking about the "PATH environment variable" instead of a "filesystem path").
So there are os.sep and os.path.sep, which are equal, as well as os.pathsep and os.path.pathsep which are also equal. Why would they do this?
32

It is os.pathsep

Comments

18

OK, so there are:

  • os.pathsep that is ; and which is a separator in the PATH environment variable;
  • os.path.sep that is / in Unix/Linux and \ in Windows, which is a separator between path components.

The similarity is a source of confusion.

3 Comments

wow that's insane. chalk up a -1 for the python filesystem author. Guido is that you?
For the slashes one can get them right under os! That's os.sep and os.altsep They're identical to the ones under os.path as listed in the docs Maybe that's a little less confusing?
There's os.pathsep and os.path.pathsep and os.sep and os.path.sep. So to make it less confusing, pick one location to use and stick to it.
14

Making it a little more explicit (For python newbies like me)

import os
print(os.pathsep)

Comments

-5

This is a sample path for your working directory/specific folder -

 import os
 my = os.pathsep+ "testImages" + os.pathsep + "imageHidden.png"
 print(my)

Output for Linux-

:testImages:imageHidden.png

Output for Windows-

;testImages;imageHidden.png

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.