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

PythonOperating SystemEnvironment Variables

Python Problem Overview


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 https://stackoverflow.com/questions/1489599/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 '/'.

Python Solutions


Solution 1 - Python

Solution 2 - Python

It is os.pathsep

Solution 3 - Python

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.

Solution 4 - Python

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

import os
print(os.pathsep)

Solution 5 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark RansomView Question on Stackoverflow
Solution 1 - PythonSilentGhostView Answer on Stackoverflow
Solution 2 - PythonDave CostaView Answer on Stackoverflow
Solution 3 - PythonDVVView Answer on Stackoverflow
Solution 4 - PythonAbbas GadhiaView Answer on Stackoverflow
Solution 5 - PythonShivam BharadwajView Answer on Stackoverflow