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 geeks del_val_one = wrg.DeleteValue(key_1, "Value One" ) # Deleting Value Two in geeks 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:
Manipulating Windows Registry using winreg in Python
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:
Contact Us