There are two ways to manipulate the registry through Python, the first of which is through the Python built-in module _winreg, and the other way is to Win32 Extension for Python's Win32API module, but additional installation is required. Here are some _winreg and Win32API demo code.
1, _winrg
Refer to the official reference documentation:
Http://docs.python.org/library/_winreg.html
Http://www.python.org/doc/2.6.2/library/_winreg.html
1.1 Read
Import _winreg
key = _winreg. Openkey (_winreg. Hkey_current_user,r "Software\microsoft\windows\currentversion\explorer")
#获取该键的所有键值 because there is no way to get the number of key values. So this is the only way to traverse
try:
i = 0
while1:
#EnumValue方法用来枚举键值, EnumKey is used to enumerate subkey
name, value, type = _winreg. Enumvalue (key, i)
print repr (name),
i +=1
except windowserror:
print
#如果知道键的名称, can also be directly evaluated
value, type = _winreg. QueryValueEx (Key, "Enableautotray")
1.2 Creating modifications
Import _winreg
key = _winreg. Openkey (_winreg. Hkey_current_user,r "Software\microsoft\windows\currentversion\explorer")
#删除键
_winreg. DeleteKey (Key, "Advanced")
#删除键值
_winreg. DeleteValue (Key, "Iconunderline")
#创建新的键
NewKey = _winreg. CreateKey (Key, "Mynewkey")
#给新创建的键添加键值
_winreg. SetValue (NewKey, "ValueName", 0, "valuecontent")
1.3 Accessing the Remote registry
1 #第二参数必须是HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE and other predefined values, get the key back after the operation can be
2 key = _winreg. Connectregisty ("IP address or machine name", _winreg.) HKEY_CURRENT_USER)
2, Win32API
Win32API and _winreg method is very similar, generally more than a prefix "Reg", the use of the method is very basically the same, here does not give specific demo code.
For general applications, using _winreg is enough, but _winreg has a problem, if Python is 32-bit, and is running on a 64-bit operating system there will be a little problem, because the operating system will be a registry redirection, A 32-bit program cannot access the registry of a 64-bit application. Before Python2.6 This problem was not resolved, in the Python2.7 in the form of patches to fix the problem (http://bugs.python.org/issue7347).
Let's illustrate this by example, assuming our operating system is 64-bit, and then installing 32-bit Python on it, look at the following Python code:
Import _winreg
key = _winreg. Openkey (_winreg. Hkey_local_machine,r "SOFTWARE\Microsoft\Windows")
NewKey = _winreg. CreateKey (Key, "Mynewkey")
The execution of the above code does not create the following key as expected:
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows"
Instead, you create the following key:
"Hkey_local_machine\software\wow6432node\microsoft\windows"
Because of the 64-bit Windows operating system, 64-bit programs and 32-bit program registry is separate, 32-bit application of the registry operations (read, write) will be redirected to Wow6432node below, 64-bit application access to the registry behavior is unchanged. So if our 32-bit applications do have access to the registry that the 64-bit program uses, there's a problem. Because of the problem with _winreg itself, its support for this situation is not sufficient, there are some bugs in the encapsulation of the Windows API, this time need to use the Win32API module.
The following method is provided by Win32API to modify the above code as follows:
Import win32api
import win32con
key = Win32API. RegCreateKeyEx (Win32con. HKEY_LOCAL_MACHINE, R "SOFTWARE\Microsoft\Windows", Win32con. Write_owner |win32con. Key_wow64_64key|win32con. key_all_access)
Win32API. RegSetValueEx (Key, "Mynewkey", 0, Win32con. REG_SZ, KeyValue)
Above the Win32con. Key_wow64_64key means direct access to the 64-bit registry, do not redirect, the default parameter is Win32con. Key_wow64_32key.