Python winreg looping through sub-keys

前端 未结 6 1427
悲哀的现实
悲哀的现实 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:31

    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
    

提交回复
热议问题