Windows registry access using Python (winreg)


As a versatile language and also availability of very large number of user supported modules, we find that python is also good at OS level programming. In this article we will see how python can access the registry of a windows operating system.

We need to import the module named winreg into the python environment.

In the below example we use the winreg module to first connect to the registry using the ConnectRegistry function and then access the registry using OpenKey function. Finally we design a for loop to print the result of the keys accessed.

Example

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

Output

Running the above code gives us the following result:

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

Updated on: 07-Jan-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements