Python _winreg woes

旧巷老猫 提交于 2019-12-04 18:42:32

问题


I'm trying to access the windows registry (in Python) to query a key value using _winreg and I can't get it to work. The following line returns a WindowsError saying that the "system cannot find the specified file":

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Autodesk\Maya\2012\Setup\InstallPath', 0, _winreg.KEY_ALL_ACCESS)

After hours of trying, it looks like Python cannot see beyond the "Maya" part of the path (it looks like the "2012\...etc..." sub-path is "invisible" or non-existent). Now I have the Registry Editor open and I guaranty there IS such a path in the HKLM. I'm on Windows 7 64bit. Any idea what I'm doing wrong? This is driving me nuts. Thanks...


回答1:


You need to combine the access key with one of the 64bit access keys.

_winreg.KEY_WOW64_64KEY Indicates that an application on 64-bit Windows should operate on the 64-bit registry view.

_winreg.KEY_WOW64_32KEY Indicates that an application on 64-bit Windows should operate on the 32-bit registry view.

Try:

_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Autodesk\Maya\2012\Setup\InstallPath', 0, (_winreg.KEY_WOW64_64KEY + _winreg.KEY_ALL_ACCESS))



回答2:


Are you also using a 64-bit version of Python, or is it a 32-bit Python? (The latter is more common.) If you're using a 32-bit version of Python, the _winreg module will see the 32-bit registry by default, while regedit will show you the 64-bit one. You should be able to tell _winreg to open a different view; see the _winreg module docs on access rights, specifically the subsection on 64-bit specific flags and the MSDN article it references. Unfortunately it doesn't look like there's a way for a 32-bit process to access the 64-bit registry, but I may be missing something.



来源:https://stackoverflow.com/questions/9348951/python-winreg-woes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!