Python Module--_winreg Operation Registry(2010-01-22 13:47:01)
reproduced
Tags: python _winreg Registry Delete key name default gateway utf-8 it |
Category: Python |
Modifying the Windows registry with a Python operation is obviously easier than using C or C + +.
Main reference: Official document: Http://docs.python.org/library/_winreg.html
There are two main ways to manipulate the registry through Python, one is through Python's built-in module _winreg, and the other is Win32 Extension for Python's Win32API module. Here is a simple look at how to operate the registry with the built-in module _winreg.
1. Read
The method of reading is the Openkey method: Open a specific key
_winreg. Openkey (Key,sub_key,res=0,sam=key_read)
Example: This example is a list of registry keys that show the native network configuration
#!/usr/bin/env python
#coding =utf-8
Import _winreg
Key = _winreg. Openkey (_winreg. HKEY_LOCAL_MACHINE, R "system\currentcontrolset\services\tcpip\parameters\interfaces\{0e184877-d910- 4877-b 4c2-04f487b6dbb7} ")
#获取该键的所有键值, iterating through enumerations
Try
I=0
While 1:
#EnumValue方法用来枚举键值,EnumKey is used to enumerate sub-keys
Name,value,type = _winreg. Enumvalue (Key,i)
Print repr (name), Value,type
I+=1
Except Windowserror:
Print
#假如知道键名, you can also take a value directly
Value,type = _winreg. QueryValueEx (Key, "DhcpDefaultGateway")
Print "Default gateway address ----", Value,type
The results of the operation are as follows:
' UseZeroBroadcast ' 0 4
' EnableDeadGWDetect ' 1 4
' EnableDHCP ' 1 4
' IPAddress ' [u ' 0.0.0.0 '] 7
' SubnetMask ' [u ' 0.0.0.0 '] 7
' DefaultGateway ' [] 7
' Defaultgatewaymetric ' [] 7
' NameServer ' 10.0.0.10 1
' Domain ' 1
' Registrationenabled ' 1 4
' Registeradaptername ' 0 4
' Tcpallowedports ' [u ' 0 '] 7
' Udpallowedports ' [u ' 0 '] 7
' Rawipallowedprotocols ' [u ' 0 '] 7
' Ntecontextlist ' [u ' 0x00000004 '] 7
' Dhcpclassidbin ' None 3
' DHCPServer ' 10.104.4.1 1
' Lease ' 907200 4
' Leaseobtainedtime ' 1264122113 4
' T1 ' 1264575713 4
' T2 ' 1264915913 4
' Leaseterminatestime ' 1265029313 4
' Ipautoconfigurationaddress ' 0.0.0.0 1
' Ipautoconfigurationmask ' 255.255.0.0 1
' Ipautoconfigurationseed ' 0 4
' AddressType ' 0 4
' Isservernapaware ' 0 4
' DhcpIPAddress ' 10.104.5.15 1
' Dhcpsubnetmask ' 255.255.254.0 1
' Dhcpretrytime ' 453598 4
' Dhcpretrystatus ' 0 4
' Dhcpnameserver ' 10.0.0.10 1
' DhcpDefaultGateway ' [u ' 10.104.4.1 '] 7
' Dhcpsubnetmaskopt ' [u ' 255.255.254.0 '] 7
Default gateway address ----[u ' 10.104.4.1 '] 7
2. Create a Modify registry
Create key:_winreg. CreateKey (Key,sub_key)
Delete key: _winreg. DeleteKey (Key,sub_key)
Delete key value: _winreg. DeleteValue (Key,value)
Assign a value to the new key: _winreg. SetValue (key,sub_key,type,value)
Example:
#!/usr/bin/env python
#coding =utf-8
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")
Go to Python module--_winreg operation Registry