1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<span id="winreg-windows-registry-access"></span><h1>winreg — Windows registry access</h1> <p>These functions expose the Windows registry API to Python. Instead of using an integer as the registry handle, a <a class="reference internal" href="#handle-object"><span class="std std-ref">handle object</span></a> is used to ensure that the handles are closed correctly, even if the programmer neglects to explicitly close them.</p> <div class="versionchanged" id="exception-changed"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Several functions in this module used to raise a <a class="reference internal" href="exceptions#WindowsError" title="WindowsError"><code>WindowsError</code></a>, which is now an alias of <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>.</p> </div> <section id="functions"> <span id="id1"></span><h2>Functions</h2> <p>This module offers the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="winreg.CloseKey">
<code>winreg.CloseKey(hkey)</code> </dt> <dd>
<p>Closes a previously opened registry key. The <em>hkey</em> argument specifies a previously opened key.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If <em>hkey</em> is not closed using this method (or via <a class="reference internal" href="#winreg.PyHKEY.Close" title="winreg.PyHKEY.Close"><code>hkey.Close()</code></a>), it is closed when the <em>hkey</em> object is destroyed by Python.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.ConnectRegistry">
<code>winreg.ConnectRegistry(computer_name, key)</code> </dt> <dd>
<p>Establishes a connection to a predefined registry handle on another computer, and returns a <a class="reference internal" href="#handle-object"><span class="std std-ref">handle object</span></a>.</p> <p><em>computer_name</em> is the name of the remote computer, of the form <code>r"\\computername"</code>. If <code>None</code>, the local computer is used.</p> <p><em>key</em> is the predefined handle to connect to.</p> <p>The return value is the handle of the opened key. If the function fails, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.ConnectRegistry</code> with arguments <code>computer_name</code>, <code>key</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.CreateKey">
<code>winreg.CreateKey(key, sub_key)</code> </dt> <dd>
<p>Creates or opens the specified key, returning a <a class="reference internal" href="#handle-object"><span class="std std-ref">handle object</span></a>.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that names the key this method opens or creates.</p> <p>If <em>key</em> is one of the predefined keys, <em>sub_key</em> may be <code>None</code>. In that case, the handle returned is the same key handle passed in to the function.</p> <p>If the key already exists, this function opens the existing key.</p> <p>The return value is the handle of the opened key. If the function fails, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.CreateKey</code> with arguments <code>key</code>, <code>sub_key</code>, <code>access</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.OpenKey/result</code> with argument <code>key</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.CreateKeyEx">
<code>winreg.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)</code> </dt> <dd>
<p>Creates or opens the specified key, returning a <a class="reference internal" href="#handle-object"><span class="std std-ref">handle object</span></a>.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that names the key this method opens or creates.</p> <p><em>reserved</em> is a reserved integer, and must be zero. The default is zero.</p> <p><em>access</em> is an integer that specifies an access mask that describes the desired security access for the key. Default is <a class="reference internal" href="#winreg.KEY_WRITE" title="winreg.KEY_WRITE"><code>KEY_WRITE</code></a>. See <a class="reference internal" href="#access-rights"><span class="std std-ref">Access Rights</span></a> for other allowed values.</p> <p>If <em>key</em> is one of the predefined keys, <em>sub_key</em> may be <code>None</code>. In that case, the handle returned is the same key handle passed in to the function.</p> <p>If the key already exists, this function opens the existing key.</p> <p>The return value is the handle of the opened key. If the function fails, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.CreateKey</code> with arguments <code>key</code>, <code>sub_key</code>, <code>access</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.OpenKey/result</code> with argument <code>key</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.DeleteKey">
<code>winreg.DeleteKey(key, sub_key)</code> </dt> <dd>
<p>Deletes the specified key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that must be a subkey of the key identified by the <em>key</em> parameter. This value must not be <code>None</code>, and the key may not have subkeys.</p> <p><em>This method can not delete keys with subkeys.</em></p> <p>If the method succeeds, the entire key, including all of its values, is removed. If the method fails, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.DeleteKey</code> with arguments <code>key</code>, <code>sub_key</code>, <code>access</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.DeleteKeyEx">
<code>winreg.DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)</code> </dt> <dd>
<p>Deletes the specified key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that must be a subkey of the key identified by the <em>key</em> parameter. This value must not be <code>None</code>, and the key may not have subkeys.</p> <p><em>reserved</em> is a reserved integer, and must be zero. The default is zero.</p> <p><em>access</em> is an integer that specifies an access mask that describes the desired security access for the key. Default is <a class="reference internal" href="#winreg.KEY_WOW64_64KEY" title="winreg.KEY_WOW64_64KEY"><code>KEY_WOW64_64KEY</code></a>. On 32-bit Windows, the WOW64 constants are ignored. See <a class="reference internal" href="#access-rights"><span class="std std-ref">Access Rights</span></a> for other allowed values.</p> <p><em>This method can not delete keys with subkeys.</em></p> <p>If the method succeeds, the entire key, including all of its values, is removed. If the method fails, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised.</p> <p>On unsupported Windows versions, <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.DeleteKey</code> with arguments <code>key</code>, <code>sub_key</code>, <code>access</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.DeleteValue">
<code>winreg.DeleteValue(key, value)</code> </dt> <dd>
<p>Removes a named value from a registry key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>value</em> is a string that identifies the value to remove.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.DeleteValue</code> with arguments <code>key</code>, <code>value</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.EnumKey">
<code>winreg.EnumKey(key, index)</code> </dt> <dd>
<p>Enumerates subkeys of an open registry key, returning a string.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>index</em> is an integer that identifies the index of the key to retrieve.</p> <p>The function retrieves the name of one subkey each time it is called. It is typically called repeatedly until an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised, indicating, no more values are available.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.EnumKey</code> with arguments <code>key</code>, <code>index</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.EnumValue">
<code>winreg.EnumValue(key, index)</code> </dt> <dd>
<p>Enumerates values of an open registry key, returning a tuple.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>index</em> is an integer that identifies the index of the value to retrieve.</p> <p>The function retrieves the name of one subkey each time it is called. It is typically called repeatedly, until an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception is raised, indicating no more values.</p> <p>The result is a tuple of 3 items:</p> <table class="docutils align-default"> <thead> <tr>
<th class="head"><p>Index</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr>
<td><p><code>0</code></p></td> <td><p>A string that identifies the value name</p></td> </tr> <tr>
<td><p><code>1</code></p></td> <td><p>An object that holds the value data, and whose type depends on the underlying registry type</p></td> </tr> <tr>
<td><p><code>2</code></p></td> <td><p>An integer that identifies the type of the value data (see table in docs for <a class="reference internal" href="#winreg.SetValueEx" title="winreg.SetValueEx"><code>SetValueEx()</code></a>)</p></td> </tr> </table> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.EnumValue</code> with arguments <code>key</code>, <code>index</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <span class="target" id="index-0"></span><dl class="py function"> <dt class="sig sig-object py" id="winreg.ExpandEnvironmentStrings">
<code>winreg.ExpandEnvironmentStrings(str)</code> </dt> <dd>
<p>Expands environment variable placeholders <code>%NAME%</code> in strings like <a class="reference internal" href="#winreg.REG_EXPAND_SZ" title="winreg.REG_EXPAND_SZ"><code>REG_EXPAND_SZ</code></a>:</p> <pre data-language="python">>>> ExpandEnvironmentStrings('%windir%')
'C:\\Windows'
</pre> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.ExpandEnvironmentStrings</code> with argument <code>str</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.FlushKey">
<code>winreg.FlushKey(key)</code> </dt> <dd>
<p>Writes all the attributes of a key to the registry.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p>It is not necessary to call <a class="reference internal" href="#winreg.FlushKey" title="winreg.FlushKey"><code>FlushKey()</code></a> to change a key. Registry changes are flushed to disk by the registry using its lazy flusher. Registry changes are also flushed to disk at system shutdown. Unlike <a class="reference internal" href="#winreg.CloseKey" title="winreg.CloseKey"><code>CloseKey()</code></a>, the <a class="reference internal" href="#winreg.FlushKey" title="winreg.FlushKey"><code>FlushKey()</code></a> method returns only when all the data has been written to the registry. An application should only call <a class="reference internal" href="#winreg.FlushKey" title="winreg.FlushKey"><code>FlushKey()</code></a> if it requires absolute certainty that registry changes are on disk.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If you don’t know whether a <a class="reference internal" href="#winreg.FlushKey" title="winreg.FlushKey"><code>FlushKey()</code></a> call is required, it probably isn’t.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.LoadKey">
<code>winreg.LoadKey(key, sub_key, file_name)</code> </dt> <dd>
<p>Creates a subkey under the specified key and stores registration information from a specified file into that subkey.</p> <p><em>key</em> is a handle returned by <a class="reference internal" href="#winreg.ConnectRegistry" title="winreg.ConnectRegistry"><code>ConnectRegistry()</code></a> or one of the constants <a class="reference internal" href="#winreg.HKEY_USERS" title="winreg.HKEY_USERS"><code>HKEY_USERS</code></a> or <a class="reference internal" href="#winreg.HKEY_LOCAL_MACHINE" title="winreg.HKEY_LOCAL_MACHINE"><code>HKEY_LOCAL_MACHINE</code></a>.</p> <p><em>sub_key</em> is a string that identifies the subkey to load.</p> <p><em>file_name</em> is the name of the file to load registry data from. This file must have been created with the <a class="reference internal" href="#winreg.SaveKey" title="winreg.SaveKey"><code>SaveKey()</code></a> function. Under the file allocation table (FAT) file system, the filename may not have an extension.</p> <p>A call to <a class="reference internal" href="#winreg.LoadKey" title="winreg.LoadKey"><code>LoadKey()</code></a> fails if the calling process does not have the <code>SE_RESTORE_PRIVILEGE</code> privilege. Note that privileges are different from permissions – see the <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx">RegLoadKey documentation</a> for more details.</p> <p>If <em>key</em> is a handle returned by <a class="reference internal" href="#winreg.ConnectRegistry" title="winreg.ConnectRegistry"><code>ConnectRegistry()</code></a>, then the path specified in <em>file_name</em> is relative to the remote computer.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.LoadKey</code> with arguments <code>key</code>, <code>sub_key</code>, <code>file_name</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.OpenKey">
<code>winreg.OpenKey(key, sub_key, reserved=0, access=KEY_READ)</code> </dt> <dt class="sig sig-object py" id="winreg.OpenKeyEx">
<code>winreg.OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)</code> </dt> <dd>
<p>Opens the specified key, returning a <a class="reference internal" href="#handle-object"><span class="std std-ref">handle object</span></a>.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that identifies the sub_key to open.</p> <p><em>reserved</em> is a reserved integer, and must be zero. The default is zero.</p> <p><em>access</em> is an integer that specifies an access mask that describes the desired security access for the key. Default is <a class="reference internal" href="#winreg.KEY_READ" title="winreg.KEY_READ"><code>KEY_READ</code></a>. See <a class="reference internal" href="#access-rights"><span class="std std-ref">Access Rights</span></a> for other allowed values.</p> <p>The result is a new handle to the specified key.</p> <p>If the function fails, <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.OpenKey</code> with arguments <code>key</code>, <code>sub_key</code>, <code>access</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.OpenKey/result</code> with argument <code>key</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Allow the use of named arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>See <a class="reference internal" href="#exception-changed"><span class="std std-ref">above</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.QueryInfoKey">
<code>winreg.QueryInfoKey(key)</code> </dt> <dd>
<p>Returns information about a key, as a tuple.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p>The result is a tuple of 3 items:</p> <table class="docutils align-default"> <thead> <tr>
<th class="head"><p>Index</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr>
<td><p><code>0</code></p></td> <td><p>An integer giving the number of sub keys this key has.</p></td> </tr> <tr>
<td><p><code>1</code></p></td> <td><p>An integer giving the number of values this key has.</p></td> </tr> <tr>
<td><p><code>2</code></p></td> <td><p>An integer giving when the key was last modified (if available) as 100’s of nanoseconds since Jan 1, 1601.</p></td> </tr> </table> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.QueryInfoKey</code> with argument <code>key</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.QueryValue">
<code>winreg.QueryValue(key, sub_key)</code> </dt> <dd>
<p>Retrieves the unnamed value for a key, as a string.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that holds the name of the subkey with which the value is associated. If this parameter is <code>None</code> or empty, the function retrieves the value set by the <a class="reference internal" href="#winreg.SetValue" title="winreg.SetValue"><code>SetValue()</code></a> method for the key identified by <em>key</em>.</p> <p>Values in the registry have name, type, and data components. This method retrieves the data for a key’s first value that has a <code>NULL</code> name. But the underlying API call doesn’t return the type, so always use <a class="reference internal" href="#winreg.QueryValueEx" title="winreg.QueryValueEx"><code>QueryValueEx()</code></a> if possible.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.QueryValue</code> with arguments <code>key</code>, <code>sub_key</code>, <code>value_name</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.QueryValueEx">
<code>winreg.QueryValueEx(key, value_name)</code> </dt> <dd>
<p>Retrieves the type and data for a specified value name associated with an open registry key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>value_name</em> is a string indicating the value to query.</p> <p>The result is a tuple of 2 items:</p> <table class="docutils align-default"> <thead> <tr>
<th class="head"><p>Index</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr>
<td><p><code>0</code></p></td> <td><p>The value of the registry item.</p></td> </tr> <tr>
<td><p><code>1</code></p></td> <td><p>An integer giving the registry type for this value (see table in docs for <a class="reference internal" href="#winreg.SetValueEx" title="winreg.SetValueEx"><code>SetValueEx()</code></a>)</p></td> </tr> </table> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.QueryValue</code> with arguments <code>key</code>, <code>sub_key</code>, <code>value_name</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.SaveKey">
<code>winreg.SaveKey(key, file_name)</code> </dt> <dd>
<p>Saves the specified key, and all its subkeys to the specified file.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>file_name</em> is the name of the file to save registry data to. This file cannot already exist. If this filename includes an extension, it cannot be used on file allocation table (FAT) file systems by the <a class="reference internal" href="#winreg.LoadKey" title="winreg.LoadKey"><code>LoadKey()</code></a> method.</p> <p>If <em>key</em> represents a key on a remote computer, the path described by <em>file_name</em> is relative to the remote computer. The caller of this method must possess the <strong>SeBackupPrivilege</strong> security privilege. Note that privileges are different than permissions – see the <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx">Conflicts Between User Rights and Permissions documentation</a> for more details.</p> <p>This function passes <code>NULL</code> for <em>security_attributes</em> to the API.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.SaveKey</code> with arguments <code>key</code>, <code>file_name</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.SetValue">
<code>winreg.SetValue(key, sub_key, type, value)</code> </dt> <dd>
<p>Associates a value with a specified key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>sub_key</em> is a string that names the subkey with which the value is associated.</p> <p><em>type</em> is an integer that specifies the type of the data. Currently this must be <a class="reference internal" href="#winreg.REG_SZ" title="winreg.REG_SZ"><code>REG_SZ</code></a>, meaning only strings are supported. Use the <a class="reference internal" href="#winreg.SetValueEx" title="winreg.SetValueEx"><code>SetValueEx()</code></a> function for support for other data types.</p> <p><em>value</em> is a string that specifies the new value.</p> <p>If the key specified by the <em>sub_key</em> parameter does not exist, the SetValue function creates it.</p> <p>Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.</p> <p>The key identified by the <em>key</em> parameter must have been opened with <a class="reference internal" href="#winreg.KEY_SET_VALUE" title="winreg.KEY_SET_VALUE"><code>KEY_SET_VALUE</code></a> access.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.SetValue</code> with arguments <code>key</code>, <code>sub_key</code>, <code>type</code>, <code>value</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.SetValueEx">
<code>winreg.SetValueEx(key, value_name, reserved, type, value)</code> </dt> <dd>
<p>Stores data in the value field of an open registry key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p><em>value_name</em> is a string that names the subkey with which the value is associated.</p> <p><em>reserved</em> can be anything – zero is always passed to the API.</p> <p><em>type</em> is an integer that specifies the type of the data. See <a class="reference internal" href="#value-types"><span class="std std-ref">Value Types</span></a> for the available types.</p> <p><em>value</em> is a string that specifies the new value.</p> <p>This method can also set additional value and type information for the specified key. The key identified by the key parameter must have been opened with <a class="reference internal" href="#winreg.KEY_SET_VALUE" title="winreg.KEY_SET_VALUE"><code>KEY_SET_VALUE</code></a> access.</p> <p>To open the key, use the <a class="reference internal" href="#winreg.CreateKey" title="winreg.CreateKey"><code>CreateKey()</code></a> or <a class="reference internal" href="#winreg.OpenKey" title="winreg.OpenKey"><code>OpenKey()</code></a> methods.</p> <p>Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.SetValue</code> with arguments <code>key</code>, <code>sub_key</code>, <code>type</code>, <code>value</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.DisableReflectionKey">
<code>winreg.DisableReflectionKey(key)</code> </dt> <dd>
<p>Disables registry reflection for 32-bit processes running on a 64-bit operating system.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p>Will generally raise <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> if executed on a 32-bit operating system.</p> <p>If the key is not on the reflection list, the function succeeds but has no effect. Disabling reflection for a key does not affect reflection of any subkeys.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.DisableReflectionKey</code> with argument <code>key</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.EnableReflectionKey">
<code>winreg.EnableReflectionKey(key)</code> </dt> <dd>
<p>Restores registry reflection for the specified disabled key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p>Will generally raise <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> if executed on a 32-bit operating system.</p> <p>Restoring reflection for a key does not affect reflection of any subkeys.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.EnableReflectionKey</code> with argument <code>key</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="winreg.QueryReflectionKey">
<code>winreg.QueryReflectionKey(key)</code> </dt> <dd>
<p>Determines the reflection state for the specified key.</p> <p><em>key</em> is an already open key, or one of the predefined <a class="reference internal" href="#hkey-constants"><span class="std std-ref">HKEY_* constants</span></a>.</p> <p>Returns <code>True</code> if reflection is disabled.</p> <p>Will generally raise <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> if executed on a 32-bit operating system.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.QueryReflectionKey</code> with argument <code>key</code>.</p> </dd>
</dl> </section> <section id="constants"> <span id="id2"></span><h2>Constants</h2> <p>The following constants are defined for use in many <a class="reference internal" href="#module-winreg" title="winreg: Routines and objects for manipulating the Windows registry. (Windows)"><code>winreg</code></a> functions.</p> <section id="hkey-constants"> <span id="id3"></span><h3>HKEY_* Constants</h3> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_CLASSES_ROOT">
<code>winreg.HKEY_CLASSES_ROOT</code> </dt> <dd>
<p>Registry entries subordinate to this key define types (or classes) of documents and the properties associated with those types. Shell and COM applications use the information stored under this key.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_CURRENT_USER">
<code>winreg.HKEY_CURRENT_USER</code> </dt> <dd>
<p>Registry entries subordinate to this key define the preferences of the current user. These preferences include the settings of environment variables, data about program groups, colors, printers, network connections, and application preferences.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_LOCAL_MACHINE">
<code>winreg.HKEY_LOCAL_MACHINE</code> </dt> <dd>
<p>Registry entries subordinate to this key define the physical state of the computer, including data about the bus type, system memory, and installed hardware and software.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_USERS">
<code>winreg.HKEY_USERS</code> </dt> <dd>
<p>Registry entries subordinate to this key define the default user configuration for new users on the local computer and the user configuration for the current user.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_PERFORMANCE_DATA">
<code>winreg.HKEY_PERFORMANCE_DATA</code> </dt> <dd>
<p>Registry entries subordinate to this key allow you to access performance data. The data is not actually stored in the registry; the registry functions cause the system to collect the data from its source.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_CURRENT_CONFIG">
<code>winreg.HKEY_CURRENT_CONFIG</code> </dt> <dd>
<p>Contains information about the current hardware profile of the local computer system.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.HKEY_DYN_DATA">
<code>winreg.HKEY_DYN_DATA</code> </dt> <dd>
<p>This key is not used in versions of Windows after 98.</p> </dd>
</dl> </section> <section id="access-rights"> <span id="id4"></span><h3>Access Rights</h3> <p>For more information, see <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx">Registry Key Security and Access</a>.</p> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_ALL_ACCESS">
<code>winreg.KEY_ALL_ACCESS</code> </dt> <dd>
<p>Combines the STANDARD_RIGHTS_REQUIRED, <a class="reference internal" href="#winreg.KEY_QUERY_VALUE" title="winreg.KEY_QUERY_VALUE"><code>KEY_QUERY_VALUE</code></a>, <a class="reference internal" href="#winreg.KEY_SET_VALUE" title="winreg.KEY_SET_VALUE"><code>KEY_SET_VALUE</code></a>, <a class="reference internal" href="#winreg.KEY_CREATE_SUB_KEY" title="winreg.KEY_CREATE_SUB_KEY"><code>KEY_CREATE_SUB_KEY</code></a>, <a class="reference internal" href="#winreg.KEY_ENUMERATE_SUB_KEYS" title="winreg.KEY_ENUMERATE_SUB_KEYS"><code>KEY_ENUMERATE_SUB_KEYS</code></a>, <a class="reference internal" href="#winreg.KEY_NOTIFY" title="winreg.KEY_NOTIFY"><code>KEY_NOTIFY</code></a>, and <a class="reference internal" href="#winreg.KEY_CREATE_LINK" title="winreg.KEY_CREATE_LINK"><code>KEY_CREATE_LINK</code></a> access rights.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_WRITE">
<code>winreg.KEY_WRITE</code> </dt> <dd>
<p>Combines the STANDARD_RIGHTS_WRITE, <a class="reference internal" href="#winreg.KEY_SET_VALUE" title="winreg.KEY_SET_VALUE"><code>KEY_SET_VALUE</code></a>, and <a class="reference internal" href="#winreg.KEY_CREATE_SUB_KEY" title="winreg.KEY_CREATE_SUB_KEY"><code>KEY_CREATE_SUB_KEY</code></a> access rights.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_READ">
<code>winreg.KEY_READ</code> </dt> <dd>
<p>Combines the STANDARD_RIGHTS_READ, <a class="reference internal" href="#winreg.KEY_QUERY_VALUE" title="winreg.KEY_QUERY_VALUE"><code>KEY_QUERY_VALUE</code></a>, <a class="reference internal" href="#winreg.KEY_ENUMERATE_SUB_KEYS" title="winreg.KEY_ENUMERATE_SUB_KEYS"><code>KEY_ENUMERATE_SUB_KEYS</code></a>, and <a class="reference internal" href="#winreg.KEY_NOTIFY" title="winreg.KEY_NOTIFY"><code>KEY_NOTIFY</code></a> values.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_EXECUTE">
<code>winreg.KEY_EXECUTE</code> </dt> <dd>
<p>Equivalent to <a class="reference internal" href="#winreg.KEY_READ" title="winreg.KEY_READ"><code>KEY_READ</code></a>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_QUERY_VALUE">
<code>winreg.KEY_QUERY_VALUE</code> </dt> <dd>
<p>Required to query the values of a registry key.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_SET_VALUE">
<code>winreg.KEY_SET_VALUE</code> </dt> <dd>
<p>Required to create, delete, or set a registry value.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_CREATE_SUB_KEY">
<code>winreg.KEY_CREATE_SUB_KEY</code> </dt> <dd>
<p>Required to create a subkey of a registry key.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_ENUMERATE_SUB_KEYS">
<code>winreg.KEY_ENUMERATE_SUB_KEYS</code> </dt> <dd>
<p>Required to enumerate the subkeys of a registry key.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_NOTIFY">
<code>winreg.KEY_NOTIFY</code> </dt> <dd>
<p>Required to request change notifications for a registry key or for subkeys of a registry key.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_CREATE_LINK">
<code>winreg.KEY_CREATE_LINK</code> </dt> <dd>
<p>Reserved for system use.</p> </dd>
</dl> <section id="bit-specific"> <span id="bit-access-rights"></span><h4>64-bit Specific</h4> <p>For more information, see <a class="reference external" href="https://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx">Accessing an Alternate Registry View</a>.</p> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_WOW64_64KEY">
<code>winreg.KEY_WOW64_64KEY</code> </dt> <dd>
<p>Indicates that an application on 64-bit Windows should operate on the 64-bit registry view. On 32-bit Windows, this constant is ignored.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.KEY_WOW64_32KEY">
<code>winreg.KEY_WOW64_32KEY</code> </dt> <dd>
<p>Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. On 32-bit Windows, this constant is ignored.</p> </dd>
</dl> </section> </section> <section id="value-types"> <span id="id5"></span><h3>Value Types</h3> <p>For more information, see <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx">Registry Value Types</a>.</p> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_BINARY">
<code>winreg.REG_BINARY</code> </dt> <dd>
<p>Binary data in any form.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_DWORD">
<code>winreg.REG_DWORD</code> </dt> <dd>
<p>32-bit number.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_DWORD_LITTLE_ENDIAN">
<code>winreg.REG_DWORD_LITTLE_ENDIAN</code> </dt> <dd>
<p>A 32-bit number in little-endian format. Equivalent to <a class="reference internal" href="#winreg.REG_DWORD" title="winreg.REG_DWORD"><code>REG_DWORD</code></a>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_DWORD_BIG_ENDIAN">
<code>winreg.REG_DWORD_BIG_ENDIAN</code> </dt> <dd>
<p>A 32-bit number in big-endian format.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_EXPAND_SZ">
<code>winreg.REG_EXPAND_SZ</code> </dt> <dd>
<p>Null-terminated string containing references to environment variables (<code>%PATH%</code>).</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_LINK">
<code>winreg.REG_LINK</code> </dt> <dd>
<p>A Unicode symbolic link.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_MULTI_SZ">
<code>winreg.REG_MULTI_SZ</code> </dt> <dd>
<p>A sequence of null-terminated strings, terminated by two null characters. (Python handles this termination automatically.)</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_NONE">
<code>winreg.REG_NONE</code> </dt> <dd>
<p>No defined value type.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_QWORD">
<code>winreg.REG_QWORD</code> </dt> <dd>
<p>A 64-bit number.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_QWORD_LITTLE_ENDIAN">
<code>winreg.REG_QWORD_LITTLE_ENDIAN</code> </dt> <dd>
<p>A 64-bit number in little-endian format. Equivalent to <a class="reference internal" href="#winreg.REG_QWORD" title="winreg.REG_QWORD"><code>REG_QWORD</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_RESOURCE_LIST">
<code>winreg.REG_RESOURCE_LIST</code> </dt> <dd>
<p>A device-driver resource list.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_FULL_RESOURCE_DESCRIPTOR">
<code>winreg.REG_FULL_RESOURCE_DESCRIPTOR</code> </dt> <dd>
<p>A hardware setting.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_RESOURCE_REQUIREMENTS_LIST">
<code>winreg.REG_RESOURCE_REQUIREMENTS_LIST</code> </dt> <dd>
<p>A hardware resource list.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="winreg.REG_SZ">
<code>winreg.REG_SZ</code> </dt> <dd>
<p>A null-terminated string.</p> </dd>
</dl> </section> </section> <section id="registry-handle-objects"> <span id="handle-object"></span><h2>Registry Handle Objects</h2> <p>This object wraps a Windows HKEY object, automatically closing it when the object is destroyed. To guarantee cleanup, you can call either the <a class="reference internal" href="#winreg.PyHKEY.Close" title="winreg.PyHKEY.Close"><code>Close()</code></a> method on the object, or the <a class="reference internal" href="#winreg.CloseKey" title="winreg.CloseKey"><code>CloseKey()</code></a> function.</p> <p>All registry functions in this module return one of these objects.</p> <p>All registry functions in this module which accept a handle object also accept an integer, however, use of the handle object is encouraged.</p> <p>Handle objects provide semantics for <a class="reference internal" href="../reference/datamodel#object.__bool__" title="object.__bool__"><code>__bool__()</code></a> – thus</p> <pre data-language="python">if handle:
print("Yes")
</pre> <p>will print <code>Yes</code> if the handle is currently valid (has not been closed or detached).</p> <p>The object also support comparison semantics, so handle objects will compare true if they both reference the same underlying Windows handle value.</p> <p>Handle objects can be converted to an integer (e.g., using the built-in <a class="reference internal" href="functions#int" title="int"><code>int()</code></a> function), in which case the underlying Windows handle value is returned. You can also use the <a class="reference internal" href="#winreg.PyHKEY.Detach" title="winreg.PyHKEY.Detach"><code>Detach()</code></a> method to return the integer handle, and also disconnect the Windows handle from the handle object.</p> <dl class="py method"> <dt class="sig sig-object py" id="winreg.PyHKEY.Close">
<code>PyHKEY.Close()</code> </dt> <dd>
<p>Closes the underlying Windows handle.</p> <p>If the handle is already closed, no error is raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="winreg.PyHKEY.Detach">
<code>PyHKEY.Detach()</code> </dt> <dd>
<p>Detaches the Windows handle from the handle object.</p> <p>The result is an integer that holds the value of the handle before it is detached. If the handle is already detached or closed, this will return zero.</p> <p>After calling this function, the handle is effectively invalidated, but the handle is not closed. You would call this function when you need the underlying Win32 handle to exist beyond the lifetime of the handle object.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>winreg.PyHKEY.Detach</code> with argument <code>key</code>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="winreg.PyHKEY.__enter__">
<code>PyHKEY.__enter__()</code> </dt> <dt class="sig sig-object py" id="winreg.PyHKEY.__exit__">
<code>PyHKEY.__exit__(*exc_info)</code> </dt> <dd>
<p>The HKEY object implements <a class="reference internal" href="../reference/datamodel#object.__enter__" title="object.__enter__"><code>__enter__()</code></a> and <a class="reference internal" href="../reference/datamodel#object.__exit__" title="object.__exit__"><code>__exit__()</code></a> and thus supports the context protocol for the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement:</p> <pre data-language="python">with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
... # work with key
</pre> <p>will automatically close <em>key</em> when control leaves the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> block.</p> </dd>
</dl> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/winreg.html" class="_attribution-link">https://docs.python.org/3.12/library/winreg.html</a>
</p>
</div>
|