Go to Python module--_winreg operation Registry

Source: Internet
Author: User
Tags delete key

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.