How to get the PATH environment-variable separator in Python?

Each Answer to this Q is separated by one/two green lines.

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 "https://stackoverflow.com/".

It is os.pathsep

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.

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

import os
print(os.pathsep)

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


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.