The Python winreg module is used to work with the windows operating system registry. winreg exposes the windows registry API to Python programming language.
What is Windows Registry?
The Windows Registry is a hierarchical database that stores low-level settings for the Microsoft Windows operating system and for applications that opt to use the registry. The kernel, device drivers, services, Security Accounts Manager, and user interfaces can all use the registry.
Example No #1
Find any application in Windows Registery with PYthon:
In this example, we are looking for a chrome application in the windows registry. We use the winreg Module to connect to the windows registry and we then try the openKey() method to look for the key. It will return the path of the application in this case it returns the chrome path.
See the Following Code Example:def _find_chrome_win():
import winreg as reg
reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
for install_type in reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE:
try:
reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
chrome_path = reg.QueryValue(reg_key, None)
reg_key.Close()
if not os.path.isfile(chrome_path):
continue
except WindowsError:
chrome_path = None
else:
break
return chrome_path
Example No #2
Get the password of a Specific Service with Python
Suppose there is a service which looks and you want to get the password of that service. You can use the following code to get the password of that specific service.
See the Following Code Example:import base64
import winreg
from . import _win_crypto
def get_password(self, service, username):
"""Get password of the username for the service
"""
try:
# fetch the password
key = self._key_for_service(service)
hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key)
password_saved = winreg.QueryValueEx(hkey, username)[0]
password_base64 = password_saved.encode('ascii')
# decode with base64
password_encrypted = base64.decodestring(password_base64)
# decrypted the password
password = _win_crypto.decrypt(password_encrypted).decode('utf-8')
except EnvironmentError:
password = None
return password
Example No #3
Get the Path of Python Installer with Python Code
You can use the following python code to get the path of the python that is installed on your system. We have used the winreg python module to do so. Below is the code that will work on a win 64. WE are getting the path from the windows registry.
See the Following Code Example:import os.path
import sys
import winreg
def _get_windows_python_path(x64):
"""Get the path to Python.exe on Windows."""
parts = str(sys.version_info.major), str(sys.version_info.minor)
ver = ''.join(parts)
dot_ver = '.'.join(parts)
if x64:
path = (r'SOFTWARE\Python\PythonCore\{}\InstallPath'
.format(dot_ver))
fallback = r'C:\Python{}\python.exe'.format(ver)
else:
path = (r'SOFTWARE\WOW6432Node\Python\PythonCore\{}-32\InstallPath'
.format(dot_ver))
fallback = r'C:\Python{}-32\python.exe'.format(ver)
try:
key = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, path)
return winreg.QueryValueEx(key, 'ExecutablePath')[0]
except FileNotFoundError:
return fallback
You Might Also Like to Read: