Reputation: 1469
I know that I can get and modify the PATH
variable by doing something like
os.environ["PATH"] += "path/to/dir".
But is there any way to ensure that the new path gets written to the beginning of PATH
?
Upvotes: 2
Views: 1785
Reputation: 595
os.environ["PATH"] = "path/to/dir" + os.pathsep + os.getenv("PATH")
https://docs.python.org/3/library/os.html
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.
os.getenv(key, default=None)
Return the value of the environment variable key if it exists, or default if it doesn’t. key, default and the result are str.
Upvotes: 3