Loop through values or registry key.. _winreg Python
Asked Answered
C

5

12

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 for the subkeys of the specified registry key.


Here Is The Code:

from _winreg import *
t = OpenKey(HKEY_CURRENT_USER, r"PATH TO KEY", 0, KEY_ALL_ACCESS)

try:
    i = 0
    while True:
        subkey = EnumValue(t, i)
        print subkey
        i += 1
except WindowsError:
    # WindowsError: [Errno 259] No more data is available    
    pass

Oh, figured it out. But, if anyone knows of another way to do it, I'll still accept that answer!

Cystocarp answered 20/10, 2010 at 1:6 Comment(0)
C
9

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
Calutron answered 20/10, 2010 at 1:13 Comment(0)
S
10

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.

Semiotics answered 10/7, 2013 at 6:5 Comment(1)
the -1 in xrange leaves out the last key. I think it should simply be for i in xrange(_winreg.QueryInfoKey(key)[0]):Ranket
C
9

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
Calutron answered 20/10, 2010 at 1:13 Comment(0)
B
2

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
Buttermilk answered 8/11, 2018 at 7:23 Comment(0)
R
0

For iterating through keys and values of registry, you would need EnumKey() and EnumValue() method from _winreg module. Note that these two methods, take index as an argument, and will provide you the key (or value) only for the given index. Therefore, in order to get all the keys (or values) you need to increment the index by one and continue until WindowsError has not encountered.

This post might help you for a detailed understanding on the same. The Github link for the code can be found in the post.

Ruffina answered 12/9, 2016 at 12:57 Comment(0)
H
0

(Python3) Using generators and recursivity as i don't like counters...

 def get_keys(self, path, i=0):
    try:
        yield winreg.EnumValue(winreg.OpenKey(winreg.HKEY_CURRENT_USER, path), i)
        yield from get_keys(path, i+1)
    except WindowsError as err:
        pass


for name, value, type in r.get_keys(r"Local Settings\Software\Microsoft\Windows\Shell\MuiCache"):
    print(f"{name} => {value} ({type})"
Hylan answered 3/12, 2019 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.