In this article, we will learn how to manipulate Windows Registry using winreg in Python.
What is windows registry?
It is a hierarchical database that holds low-level settings for the windows operating system and for applications in which the use of a registry is required. The device drivers, kernel, services, user interfaces, and security accounts manager can all use the registry.
IMPORTANT: As Windows Registry is a very sensitive place to do anything, do not change any value predefined by the Windows System. This article will just be for learning purposes of how Python can be used to create some new registry keys with user-defined values.
Windows registry can be used for different purposes, it can be accessed via typing regedit in the Run application.
Window opens after running “regedit” in run:
Creating a new Key and Assigning a new value to it.
We will be creating a new key with a user-defined value under the HKEY_CURRENT_USER as that holds information about the current user only, not many confidential system files or information, HKEY_LOCAL_MACHINE holds the information related to the system and it is better not to tamper with them or create anything new which can affect the system.
winreg module is a built-in module that installs along with Python. so, we have to just import it.
Python3
# importing required module import winreg as wrg # Store location of HKEY_CURRENT_USER location = wrg.HKEY_CURRENT_USER # Store path in soft soft = wrg.OpenKeyEx(location, r "SOFTWARE\\" ) key_1 = wrg.CreateKey(soft, "Geeks" ) # Creating values in Geeks wrg.SetValueEx(key_1, "value One" , 0 , wrg.REG_SZ, "Lazyroar is Best" ) wrg.SetValueEx(key_1, "value Two" , 0 , wrg.REG_SZ, "Participate in Technical Scripter" ) if key_1: wrg.CloseKey(key_1) |
Explanation of the Above Code:
- Firstly importing the module winreg with the alias wrg, users can use anything if they want or can use winreg also every time.
- Then defining the location where we would be creating the key and assign value.
- Then in “soft” variable passing the location and the name of the subfolder inside OpenKeyEx() method of winreg, which is used to open already made keys, it takes two arguments, first one the main folder name and then the subfolder mentioned as ‘key’ here.
- Then we are creating a new key whose name is Geeks inside the Software key.
- Then last two lines are providing some values and name, the REG_SZ is mentioned as we want to use a fixed-length string here, to keep it as simple as possible.
- After creating and assigning values it is time to close the key otherwise it will remain open and other operations can’t be done. It’s like file handling.
Output:
If we open the regedit again then we will see the following output.
Note: To see the changes of anything like the creation, or deletion of keys user need to open the registry editor every time or refresh it.
Reading the contents of the created key.
Now after creating and assigning values it is time to fetch them, for that we will use the method QueryValueEx method and pass the exact location of the keys and a string that acts as a value to query. then we will close the key and print the two variables which were used to store the fetched values.
Python3
# importing required module import winreg as wrg # Store location of HKEY_CURRENT_USER location = wrg.HKEY_CURRENT_USER # Storing path in soft soft = wrg.OpenKeyEx(location,r "SOFTWARE\\Geeks\\" ) # reading values in value_1 and value_2 value_1 = wrg.QueryValueEx(soft, "Value One" ) value_2 = wrg.QueryValueEx(soft, "Value Two" ) # Closing folder if soft: wrg.CloseKey(soft) # Printing values print (value_1) print (value_2) |
Output:
Delete a particular value from a Key
Now we will delete that particular key that we made earlier. But if a key has some value associated with it then we can’t delete the Key directly, we first have to delete the value. Using the below code we can delete the values. Here I will be deleting just one to show how it works, user might delete all the values if they want.
Python3
# import required module import winreg as wrg # Store location of HKEY_CURRENT_USER location = wrg.HKEY_CURRENT_USER # Store path in soft soft = wrg.OpenKeyEx(location, r "SOFTWARE\\" ) key_1 = wrg.CreateKey(soft, "Geeks" ) # Deleting Value One in Lazyroar del_val_one = wrg.DeleteValue(key_1, "Value One" ) # Deleting Value Two in Lazyroar del_val_two = wrg.DeleteValue(key_1, "Value Two" ) # Closing folder if key_1: wrg.CloseKey(key_1) |
The above code deletes both the values, if the user wants they can only delete one, then after refreshing the registry editor there must be one value left.
Note: Remember to close the key after deletion otherwise this will not work as expected.
Output:
Deleting the Key
Now after clearing the key (deleting the values) it is time to Delete the key itself.
Python3
# importing required module import winreg as wrg # Store location of HKEY_CURRENT_USER location = wrg.HKEY_CURRENT_USER # Store complete path in soft soft = wrg.OpenKeyEx(location, r "SOFTWARE\\" ) key_1 = wrg.CreateKey(soft, "Geeks" ) # Deleting the foler Geeks del_key = wrg.DeleteKey(key_1, "") # Closing folder if key_1: wrg.CloseKey(key_1) |
DeleteKey also takes two parameters one is the key and the second one is the subkey, as we have already removed all those subkeys we have passed a blank string here (remember don’t even use space, it will be considered as a character).
Output: No Geeks key is there anymore.