Here is what I want to achieve:
I am coding a Python based software, which will need to append new directories to PATH in environment variables in Windows. In order to do that, I first get the path, then modify the string, and last use SETX to update the new PATH.
My problem:
I tried three methods to get PATH (with python or cmd), but they all returns me the combination of USER PATH and SYSTEM PATH. The three methods are:
os.environ['PATH']
os.system('echo %PATH%')
os.system('set PATH')
reg query HKCU\Environment /v PATH
import _winreg import unicodedata keyQ = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Environment', 0, _winreg.KEY_QUERY_VALUE) path_old, _ = _winreg.QueryValueEx(keyQ, "PATH") #the result is unicode, need to be converted unicodedata.normalize('NFKD', path_old).encode('ascii','ignore')
Although I said I want an answer without access to registry, it turns out this is the only way to get user environment variable "PATH". Thank you everyone.