34.3. _winreg – Windows registry access

Note

The _winreg module has been renamed to winreg in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

New in version 2.0:

New in version 2.0.

These functions expose the Windows registry API to Python. Instead of using an integer as the registry handle, a handle object is used to ensure that the handles are closed correctly, even if the programmer neglects to explicitly close them.

This module offers the following functions:

_winreg.CloseKey(hkey)

Closes a previously opened registry key. The hkey argument specifies a previously opened key.

Note

If hkey is not closed using this method (or via hkey.Close()), it is closed when the hkey object is destroyed by Python.

_winreg.ConnectRegistry(computer_name, key)

Establishes a connection to a predefined registry handle on another computer, and returns a handle object.

computer_name is the name of the remote computer, of the form r"\\computername". If None, the local computer is used.

key is the predefined handle to connect to.

The return value is the handle of the opened key. If the function fails, a WindowsError exception is raised.

_winreg.CreateKey(key, sub_key)

Creates or opens the specified key, returning a handle object.

key is an already open key, or one of the predefined HKEY_* constants.

sub_key is a string that names the key this method opens or creates.

If key is one of the predefined keys, sub_key may be None. In that case, the handle returned is the same key handle passed in to the function.

If the key already exists, this function opens the existing key.

The return value is the handle of the opened key. If the function fails, a WindowsError exception is raised.

_winreg.DeleteKey(key, sub_key)

Deletes the specified key.

key is an already open key, or any one of the predefined HKEY_* constants.

sub_key is a string that must be a subkey of the key identified by the key parameter. This value must not be None, and the key may not have subkeys.

This method can not delete keys with subkeys.

If the method succeeds, the entire key, including all of its values, is removed. If the method fails, a WindowsError exception is raised.

_winreg.DeleteValue(key, value)

Removes a named value from a registry key.

key is an already open key, or one of the predefined HKEY_* constants.

value is a string that identifies the value to remove.

_winreg.EnumKey(key, index)

Enumerates subkeys of an open registry key, returning a string.

key is an already open key, or any one of the predefined HKEY_* constants.

index is an integer that identifies the index of the key to retrieve.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly until a WindowsError exception is raised, indicating, no more values are available.

_winreg.EnumValue(key, index)

Enumerates values of an open registry key, returning a tuple.

key is an already open key, or any one of the predefined HKEY_* constants.

index is an integer that identifies the index of the value to retrieve.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly, until a WindowsError exception is raised, indicating no more values.

The result is a tuple of 3 items:

Index Meaning
0 A string that identifies the value name
1 An object that holds the value data, and whose type depends on the underlying registry type
2 An integer that identifies the type of the value data (see table in docs for SetValueEx())
_winreg.ExpandEnvironmentStrings(unicode)

Expands environment variable placeholders %NAME% in unicode strings like REG_EXPAND_SZ:

>>> ExpandEnvironmentStrings(u"%windir%")
u"C:\\Windows"

New in version 2.6:

New in version 2.6.