[Tutor] Advice on Python codes - A recursive function to output entire directory subkeys of Windows Registry

Alan Ho alanho_80 at yahoo.com.sg
Wed Feb 5 16:30:35 CET 2014


Hi,

I am a novice in Python, having attended a course few weeks ago and I'm working on my assignment now, and I encounter this issue when I was trying to print the entire Windows Registry (WR) sub-keys directories (trying that first with the below codes with adaptations from on-line research) and its value (later on).

Kindly advise if any, as it has been taking me few days. What I need is a display on the std screen the entire directory of sub-keys (inserted into a list) in a WR key say, HKEY_CURRENT_FIG, and then I will write the contents into a text or CSV file.
Thanks advance!

Here are my codes,

-----------------
import winreg

def traverse(root, key, list):
    hKey = winreg.OpenKey(root, key)
    try:
        i = 0
        while True:
            strFullSubKey = ""
            try:
                strSubKey = winreg.EnumKey(hKey, i)
                if (key != ""):
                    strFullSubKey = key + "\\" + strSubKey
                else:
                    strFullSubKey = strSubKey
            except WindowsError:
                hKey.Close()
                return
            traverse(root, strFullSubKey, list)
            list.append(strFullSubKey)
            i += 1

    except  WindowsError:
        hKey.Close()

global list
list = list()
traverse (winreg.HKEY_CURRENT_CONFIG,"",list)
print (list)

-----------------

results on screen, which is not very correct, seeing that the 2nd item ("Software") on the list should not be printed/inside in the first place, as it is not as such in the actual directory structure of HKEY_CURRENT_CONFIG

['Software\\Fonts', 'Software', 'System\\CurrentControlSet\\Control\\Print\\Printers\\HP Deskjet F300 Series', 'System\\CurrentControlSet\\Control\\Print\\Printers', 'System\\CurrentControlSet\\Control\\Print', 'System\\CurrentControlSet\\Control\\VIDEO', 'System\\CurrentControlSet\\Control', 'System\\CurrentControlSet\\SERVICES\\TSDDD\\DEVICE0', 'System\\CurrentControlSet\\SERVICES\\TSDDD', 'System\\CurrentControlSet\\SERVICES\\VGASAVE\\DEVICE0', 'System\\CurrentControlSet\\SERVICES\\VGASAVE', 'System\\CurrentControlSet\\SERVICES', 'System\\CurrentControlSet', 'System']
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140205/77a76df7/attachment-0001.html>


More information about the Tutor mailing list