Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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 exceptinto 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top