Python Windows 10注册表中路径项的地址?

Python Windows 10注册表中路径项的地址?,python,windows,path,registry,winreg,Python,Windows,Path,Registry,Winreg,Windows 10注册表中用于获取和设置PATH变量的正确地址是什么 当我运行下面的Python程序时,以下三个选项都不起作用: from winreg import * #The following line (uncommented) gives a list of things including Environment keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager" #The following line

Windows 10注册表中用于获取和设置PATH变量的正确地址是什么

当我运行下面的Python程序时,以下三个选项都不起作用:

from winreg import *
#The following line (uncommented) gives a list of things including Environment  
keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager"  
#The following line (uncommendted) gives an empty list of results (nothing)  
#keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"  
#The following line (uncommented) gives an error message as follows: "FileNotFoundError: [WinError 2] The system cannot find the file specified"  
#keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path"  
aKey = OpenKey(HKEY_LOCAL_MACHINE, keyVal, 0, KEY_ALL_ACCESS)  
try:  
    i = 0  
    while True:  
        asubkey = EnumKey(aKey, i)  
        print(asubkey)  
        i += 1  
except WindowsError:  
    pass  
调用上面的Python代码时,Windows CMD正在以管理员身份运行。我以管理员身份运行Windows CMD,希望在运行上述脚本访问注册表项时避免权限问题

注意:当我在Windows开始菜单中键入
regedit
并向下搜索到
Computer\HKEY\u LOCAL\u MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
时,我能够看到正确填充的
路径
变量。即使上面显示的错误是在我以管理员身份尝试以编程方式访问此文件时给出的


第二条注意:当我按照@SimonCrane的建议尝试代码,并使用
打开环境注册表项('system')
调用函数时,当
SYS\u env\u SUBPATH=r“system\CurrentControlSet\Control\Session Manager\Environment\Path”
时,结果是一个错误,或者使用
SYS\u env\u SUBPATH=r时根本没有结果“SYSTEM\CurrentControlSet\Control\Session Manager\Environment”或“SYSTEM\CurrentControlSet\Control\Session Manager\Environment”或“SYSTEM\CurrentControlSet\Control\Session Manager”


当您可以通过
os.environ
访问环境变量时,为什么要从注册表中获取它?@mikewatt,因为
os.environ
仅在调用脚本运行时临时更改
路径。相反,写入注册表是必要的,以便“永久地”"修改
路径
。查看一下@SimonCrane我查看了你的链接,并在这篇文章的末尾发布了结果。你还有什么建议吗?当你可以通过
os.environ
访问环境变量时,为什么要从注册表中获取它?@mikewatt,因为
os.environ
只会更改
路径
在调用脚本运行时暂时停止。相比之下,为了“永久”修改
路径
,必须向注册表中写入内容。请查看@SimonCrane。我查看了您的链接,并在本操作的末尾发布了结果。您还有什么建议?