#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2017/10/19 14:38
# @Author : otfsenter
# @File : operation_registry.py
import _winreg
def delete_sub_key(key0, key1, key2=""):
if key2 == "":
current_key = key1
else:
current_key = key1 + "\\" + key2
open_key = _winreg.OpenKey(key0, current_key, 0, _winreg.KEY_ALL_ACCESS)
info_key = _winreg.QueryInfoKey(open_key)
# print 'info_key: ', info_key
for x in range(0, info_key[0]):
# NOTE:: This code is to delete the key and all subkeys.
# If you just want to walk through them, then
# you should pass x to EnumKey. sub_key = _winreg.EnumKey(open_key, x)
# Deleting the sub_key will change the SubKey count used by EnumKey.
# We must always pass 0 to EnumKey so we
# always get back the new first SubKey.
sub_key = _winreg.EnumKey(open_key, 0)
print 'sub_key: ', sub_key
try:
_winreg.DeleteKey(open_key, sub_key)
print "Removed %s\\%s " % (current_key, sub_key)
except:
delete_sub_key(key0, current_key, sub_key)
# no extra delete here since each call
# to deleteSubkey will try to delete itself when its empty.
_winreg.DeleteKey(open_key, "")
open_key.Close()
print "Removed %s" % current_key
return
# delete_sub_key(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft", "Internet Explorer")
delete_sub_key(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Explorer")
delete_sub_key(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")