ColorTable00 0 4
ColorTable01 8388608 4
ColorTable02 32768 4
ColorTable03 8421376 4
ColorTable04 128 4
ColorTable05 8388736 4
ColorTable06 32896 4
ColorTable07 12632256 4
ColorTable08 8421504 4
ColorTable09 16711680 4
ColorTable10 65280 4
ColorTable11 16776960 4
ColorTable12 255 4
ColorTable13 16711935 4
ColorTable14 65535 4
ColorTable15 16777215 4
CtrlKeyShortcutsDisabled 0 4
CursorSize 25 4
EnableColorSelection 0 4
ExtendedEditKey 1 4
ExtendedEditKeyCustom 0 4
FaceName DefaultTTFont 1
FilterOnPaste 1 4
FontFamily 0 4
FontSize 1048576 4
FontWeight 0 4
ForceV2 1 4
FullScreen 0 4
HistoryBufferSize 50 4
HistoryNoDup 0 4
InsertMode 1 4
LineSelection 1 4
LineWrap 1 4
LoadConIme 1 4
NumberOfHistoryBuffers 4 4
PopupColors 245 4
QuickEdit 1 4
ScreenBufferSize 589889656 4
ScreenColors 7 4
ScrollScale 1 4
TrimLeadingZeros 0 4
WindowAlpha 255 4
WindowSize 1966200 4
WordDelimiters 0 4
[Error 259]


## 二、增加和删除键值

- `CreateKey(key, sub_key)`:创建一个新键,如果键存在就不会创建。
- `SetValueEx(key, value_name, reserved, type, value)`:给一个键值赋值,如果键不存在将会自动创建。
- `SetValue(key, sub_key, type, value)`:设置一个子键,并给予一个默认值`value`。
- `DeleteKey(key, sub_key)`:删除键

示例:

key = reg.OpenKey(reg.HKEY_CURRENT_USER, "Console")
try:

newKey = reg.CreateKey(key, r"test")  # 创建一个子键
reg.SetValueEx(newKey, r"newKey", 0, reg.REG_SZ, "HelloWorld") 
reg.SetValue(newKey, r"newSubKey", reg.REG_SZ, "Hey")

except Exception as e:

print e

运行后的结果:

![](https://i.maqian.xin/2017/12/20171203121447.png)

## 三、问题

### 3.1 WindwosError: Error 5

没有权限导致的,`OpenKey` 中的`sam` 变量表明了操作的权限,默认是`KEY_READ` ,改成`KEY_ALL_ACCESS` 然后以管理员身份运行即可。例如:

KEY = r"SYSTEMControlSet001ControlClass{4d36e968-e325-11ce-bfc1-08002be10318}"
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, KEY, 0, _winreg.KEY_ALL_ACCESS)

最后修改:2017 年 12 月 03 日
如果觉得我的文章对你有用,请随意赞赏