Python winreg looping through sub-keys

眉间皱痕 提交于 2020-07-03 02:24:41

问题


I'm able to successfully retrieve the 5 sub-keys from my windows 7 machine registry hive "HKEY_LOCAL_MACHINE" with the code below.

from _winreg import *

try:
    i = 0
    while True:
        subkey = EnumKey(HKEY_LOCAL_MACHINE, i)
        print subkey
        i += 1
except WindowsError:
    pass

My question is, how do I then enumerate the keys under those? I want to end up listing all the keys in the SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged folder but I can't figure out how to step my way down there.

In response to the first comment, I ran this code on my machine and while it didn't error out, it didn't produce results.

from _winreg import *

aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged")
for i in range(1024):
    try:
        keyname = EnumKey(aKey, i)
        asubkey = OpenKey(aKey, keyname)
        val = QueryValueEx(asubkey, "Description")
        print val
    except WindowsError:
        break

A regedit or reg query shows 6 values in that folder but I can't get a python script to show me those six.


回答1:


Just want to add a perhaps more pythonic solution.

from _winreg import *
from contextlib import suppress
import itertools

def subkeys(path, hkey=HKEY_LOCAL_MACHINE, flags=0):
    with suppress(WindowsError), OpenKey(hkey, path, 0, KEY_READ|flags) as k:
        for i in itertools.count():
            yield EnumKey(k, i)

You can now access the keys as expected

for key in subkeys(r'path\to\your\key'):
    print key

For python versions < 3.4 that lack suppress(), I recommend adding it to your project:

from contextlib import contextmanager

@contextmanager
def suppress(*exceptions):
    try:
        yield
    except exceptions:
        pass

Note: If you have trouble reading some values you might be reading from the wrong registry view. Pass KEY_WOW64_64KEY or KEY_WOW64_32KEY to the flags parameter). Using OpenKey() as context manager was introduced in python 2.6.




回答2:


Does something like this work?

import _winreg

def subkeys(key):
    i = 0
    while True:
        try:
            subkey = _winreg.EnumKey(key, i)
            yield subkey
            i+=1
        except WindowsError:
            break

def traverse_registry_tree(key=_winreg.HKEY_LOCAL_MACHINE, tabs=0):
    for k in subkeys(key):
        print '\t'*tabs + str(k)
        traverse_registry_tree(k, tabs+1)



回答3:


I don't have the same registry keys to search but the following code will list all the subkeys in HKEY_LOCAL_MACHINE\Software. I think if you change the value of the keyVal string to your directory it will work.

The try ... except bloc is this way because EnumKey will fail. I didn't do it as a for loop because I dont know how to get the correct length of aKey.

keyVal = r"Software"
aKey = OpenKey(HKEY_LOCAL_MACHINE, keyVal, 0, KEY_ALL_ACCESS)
try:
    i = 0
    while True:
        asubkey = EnumKey(aKey, i)
        print(asubkey)
        i += 1
except WindowsError:
    pass



回答4:


This works, and prints out the list of all subkeys (fixed version of @Broseph's answer)

import _winreg

def subkeys(key):
    i = 0
    while True:
        try:
            subkey = _winreg.EnumKey(key, i)
            yield subkey
            i+=1
        except WindowsError as e:
            break

def traverse_registry_tree(hkey, keypath, tabs=0):
    key = _winreg.OpenKey(hkey, keypath, 0, _winreg.KEY_READ)
    for subkeyname in subkeys(key):
        print '\t'*tabs + subkeyname
        subkeypath = "%s\\%s" % (keypath, subkeyname)
        traverse_registry_tree(hkey, subkeypath, tabs+1)

keypath = r"SOFTWARE\\Microsoft\\Windows"

traverse_registry_tree(_winreg.HKEY_LOCAL_MACHINE, keypath)



回答5:


For iterating through keys of Windows registry, you would need EnumKey() from _winreg module. Given below is the definition for EnumKey() :-

def EnumKey(key, index):

  • Enumerates subkeys of an open registry key.
  • key is an already open key, or any one of the predefined HKEY_* constants.
  • index is an integer that identifies the index of the key to retrieve.

Note that this method, takes index as an argument, and will provide you the key only for the given index. Therefore, in order to get all the keys, you need to increment the index by one and continue until you encounter WindowsError.

Refer to this post for a detailed understanding on the same. The Github link for the code can be found in the post.



来源:https://stackoverflow.com/questions/14350517/python-winreg-looping-through-sub-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!