使用Python的Windows注册表访问(Winreg)

作为一种通用的语言,以及大量用户支持的模块的可用性,我们发现python在操作系统级别的编程方面也很擅长。在本文中,我们将看到python如何访问Windows操作系统的注册表。

我们需要将名为winreg的模块导入python环境。

在下面的示例中,我们使用winreg模块首先使用ConnectRegistry函数连接到注册表,然后使用OpenKey函数访问注册表。最后,我们设计了一个for循环来打印所访问键的结果。

示例

import winreg
#connecting to key in registry
access_registry = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)

access_key = winreg.OpenKey(access_registry,r"SOFTWARE\Microsoft\Windows\CurrentVersion")
#accessing the key to open the registry directories under
for n in range(20):
   try:
      x =winreg.EnumKey(access_key,n)
      print(x)
   except:
      break

输出结果

运行上面的代码将为我们提供以下结果:

ApplicationFrame
AppModel
Appx
Audio
Authentication
AutoRotation
BITS
Casting
ClosedCaptioning
CloudExperienceHost
Component Based Servicing
……..
…..