How would I loop through all the values of a Windows Registry Key using the Python module _winreg. I have code that will do what I want, but it is
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.