Loop through values or registry key.. _winreg Python

I prefer to avoid the error instead of diving right into it ...

Use _winreg.QueryInfoKey to get the number of values:

import _winreg
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'PATH\TO\KEY', 0, _winreg.KEY_READ)

for i in xrange(0, _winreg.QueryInfoKey(key)[1]):
    print _winreg.EnumValue(key, i)

To get the number of Keys, same method, different index (second half of original question):

for i in xrange(0, _winreg.QueryInfoKey(key)[0]):
    print _winreg.EnumKey(key, i)

Note: use import instead of from ... import to make it explicit where functions and variables are coming from. Makes it easier to follow code later in life.


for python 3

import winreg
hKey = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache")


try:
    count = 0
    while 1:
        name, value, type = winreg.EnumValue(hKey, count)
        print (name),
        count = count + 1
except WindowsError as err:
    print(err)
    pass

Shouldn't EnumValue be of help here

# list all values for a key
try:
    count = 0
    while 1:
        name, value, type = _winreg.EnumValue(t, count)
        print repr(name),
        count = count + 1
except WindowsError:
    pass