bartoli
bartoli

Reputation: 173

Portable way to set PATH environment variable?

I have been having issues with a python script that sets PATH environment variable. The script was written to be run from a 'native' windows python, but due to some mistake from my part, i ran it from a 'cygwin' windows python. So i was setting in my script the PATH environment variable with paths separated by ";", when the cygwin python needs ":" separated paths to understand it.

I'd like to make the script run in both python in the future. So, would there be a method, 'equivalent' of os.path.join(), that would concatenate paths with the correct delimiter depending on how the python version understands the PATH variable (and that possibly would use an equivalent of cygpath command to convert path formats if needed)

Upvotes: 0

Views: 880

Answers (1)

bereal
bereal

Reputation: 34312

If I understand you correct, os.pathsep is what you need:

import os
os.pathsep.join(['dir1', 'dir2'])

Will result in dir1:dir2 in Unix and Cygwin and dir1;dir2 in Windows

Upvotes: 2

Related Questions