Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 512
Mark Ransom Avatar asked Sep 30 '09 15:09

Mark Ransom


People also ask

How do I access the PATH variable?

Select Start select Control Panel. double click System and select the Advanced tab. Click Environment Variables. In the section System Variables find the PATH environment variable and select it.


4 Answers

os.pathsep

like image 178
SilentGhost Avatar answered Oct 12 '22 04:10

SilentGhost


It is os.pathsep

like image 40
Dave Costa Avatar answered Oct 12 '22 03:10

Dave Costa


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.

like image 36
DVV Avatar answered Oct 12 '22 04:10

DVV


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

import os
print(os.pathsep)
like image 24
Abbas Gadhia Avatar answered Oct 12 '22 04:10

Abbas Gadhia