Skip to content

Instantly share code, notes, and snippets.

@michaeltrainor
Created August 30, 2017 08:54
Show Gist options
  • Save michaeltrainor/8d3073e4c393bb4046bfdca18be806d5 to your computer and use it in GitHub Desktop.
Save michaeltrainor/8d3073e4c393bb4046bfdca18be806d5 to your computer and use it in GitHub Desktop.
Hey there! Here's a quick example of how to query the Windows registry via the standard winreg library.
# standard libraries
import platform
import _winreg
def query(key, sub_key, path):
result = None
if platform.architecture()[0] == "32bit":
architecture_ = _winreg.KEY_READ | _winreg.KEY_WOW64_32KEY
if platform.architecture()[0] == "64bit":
architecture_ = _winreg.KEY_READ | _winreg.KEY_WOW64_32KEY
try:
registry_key = _winreg.OpenKey(key, sub_key, 0, architecture_)
value, regtype = _winreg.QueryValueEx(registry_key, path)
_winreg.CloseKey(registry_key)
result = value
except (WindowsError, IOError):
result = None
finally:
return result
def local_machine_query(sub_key, path):
return query(_winreg.HKEY_LOCAL_MACHINE, sub_key, path)
def current_user_query(sub_key, path):
return query(_winreg.HKEY_CURRENT_USER, sub_key, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment