Reputation: 447
I'm looking for a way to check if a registry key exists with python.
How can I do this or what code do I need to check if a registry key exists or not?
Upvotes: 3
Views: 18934
Reputation: 5159
AFAIK you can only use a try/except combo, at least in winregistry
.
For instance:
#!/usr/bin/env python3.9
import winreg as reg
key_path = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Taskband\\NumThumbnails"
try:
key = reg.OpenKey(reg.HKEY_CURRENT_USER, key_path)
return reg.QueryValue(key, "")
except FileNotFoundError:
print("Key does not exist")
You could for instance create wrappers for the different registries:
def get_user_registry_key(key_path) -> Optional[reg.HKEYType]:
try:
return reg.OpenKey(reg.HKEY_CURRENT_USER, key_path)
except FileNotFoundError:
return None
if key := get_user_registry_key(key_path):
print(reg.QueryValue(key, ""))
Upvotes: 1
Reputation: 109
This is an older post but I felt after some similar searching, thought I would add some information to this. winreg
, from what I heave found, only works on a Windows Environment. python-registry by williballenthin can be used to do this cross-platform and has a multitude of great options when working with the Registry.
If you have a target key that has values you want to pull out, you can list them out following these steps....First, import modules (pip install python-registry). This may not work as the master folder gets inserted into the libs/sitepackages, make sure the Registry folder is at the root of the site-packages.
from Registry import Registry # Ensure Registry is in your libs/site-packages
Next create your function and make sure you add in a try:
and except
into your function to check if its there.
# Store internal Registry paths as variable, may make it easier, remove repeating yourself
time_zone = "ControlSet001\\Control\\TimeZoneInformation"
# STORE the path to your files if you plan on repeating this process.
<Path_To_reg_hive> = "C:\\Users\\Desktop\\Whatever_Folder\\SYSTEM"
def get_data():
registry = Registry.Registry(<Path_To_reg_hive>) # Explicitly, or use variable above
try:
key = registry.open(time_zone) # Registry.Registry opens reg file, goes to the path (time_zone)
except Registry.RegistryKeyNotFoundException: # This error occurs if Path is not present
print("Sorry Bud, no key values found at : " + time_zone) # If not there, print a response
You can make a dict of everything you want to check and just iterate through it with this process to check several at once, or just one at a time. Here is a working example:
from Registry import Registry
# These can be included directly into the function, or separated if you have several
system = "SYSTEM" # or a Path, I have a SYSTEM hive file in my working environment folder
time_zone = "ControlSet001\\Control\\TimeZoneInformation123" # Path you want to check, added 123 so its not there
def get_data():
registry = Registry.Registry(system) # Explicitly, or use variable above
try:
key = registry.open(time_zone) # Registry.Registry opens reg file, goes to the path (time_zone)
except Registry.RegistryKeyNotFoundException: # This error occurs if Path is not present
print("Sorry Bud, no key values found at : " + time_zone) # If not there, print a response
# key is identified above only to use later, such as.... for v in key.subkeys(): Do more stuff
get_data()
Which returns,
Sorry Bud, no key values found at : ControlSet001\Control\TimeZoneInformation123
Upvotes: 2
Reputation: 156
There appears to be some information in a previous answer here.
Are you checking for its existence because you want your program to read it? To check for the existence of they key, you can wrap it in a try-except
block. This will prevent "race conditions" trying to read the key, in the (unlikely) event it is modified between checking for its existence and actually reading the key. Something like:
from _winreg import *
key_to_read = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
try:
reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
k = OpenKey(reg, key_to_read)
# do things with the key here ...
except:
# do things to handle the exception here
Upvotes: 4