Welcome, guest | Sign In | My Account | Store | Cart

The code presented below is a short example of how to use winreg. This recipe prints out as much data as possible about the user's registry. Hives are specified as such before their representations. Errors in opening keys are listed with the problem key's name.

Python, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from winreg import *

def main():
    for hive in Registry():
        show_all(hive)

def show_all(key, level=0):
    if level:
        title(repr(key), level)
    else:
        title('HIVE ' + repr(key), level)
    for name in key.values:
        if name:
            point('%r = %r' % (name, key.values[name]), level + 1)
        else:
            point('(Default) = %r' % key.values[name], level + 1)
    for name in key.keys:
        try:
            show_all(key.keys[name], level + 1)
        except WindowsError:
            title('ERROR: %s' % name, level + 1)

def title(string, level):
    point(string, level)
    point('=' * len(string), level)

def point(string, level):
    print '  ' * level + string

if __name__ == '__main__':
    main()

This recipe demonstrates sample usage of the winreg module. It is designed to give a representation of the registry's state.