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?
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