Python:winreg模块:Windows 7:None不是有效的HKEY

2024-09-28 22:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在读取windows 7 winth winreg模块的注册表值时遇到问题。有没有解决相同问题的指针?在

代码:

try:
    ParentKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
    i = 0
    while 1:
        name, value, type = _winreg.EnumValue(ParentKey, i)
        print repr(name),
        i += 1

except Exception as e:
    print(Exception(e))

ParentKey =_winreg.DisableReflectionKey(ParentKey)    
temp = _winreg.QueryValueEx(ParentKey, 'DisplayName')
temp1 = _winreg.QueryValueEx(ParentKey, 'DisplayVersion')
temp2 = _winreg.QueryValueEx(ParentKey, 'Publisher')
temp3 = _winreg.QueryValueEx(ParentKey, 'InstallLocation')

display = str(temp[0])
display_ver=str(temp1[0])
display_p=str(temp2[0])
display_loc=str(temp3)
print ('Display Name: ' + display + '\nDisplay version:  ' + display_ver + '\nVendor/Publisher:  ' + display_p +'\nRegkey: ' + display_loc +'\nInstall Location: ' )

输出:

^{pr2}$

Tags: namedisplayexceptiontemplocpublisherprintstr
1条回答
网友
1楼 · 发布于 2024-09-28 22:21:50

这条线:

ParentKey = _winreg.DisableReflectionKey(ParentKey)

将返回None。函数DisableReflectionKey没有记录为返回任何内容(成功或失败由是否引发异常来指示)。这种不返回任何内容的函数隐式返回None。由于您将返回值绑定到ParentKey,因此该变量将从该点起保存None。在

所以,当然,接下来的电话

^{pr2}$

将失败,因为QueryValueEx需要定义的键(而不是None)才能工作。在

相关问题 更多 >