Python winreg looping through sub-keys

前端 未结 6 1384
悲哀的现实
悲哀的现实 2021-01-12 05:16

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 *

         


        
6条回答
  •  离开以前
    2021-01-12 05:44

    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)
    

提交回复
热议问题