[Tutor] Advice on Python codes - A recursive function to output entire directory subkeys of Windows Registry
Tim Golden
mail at timgolden.me.uk
Wed Feb 5 19:01:31 CET 2014
On 05/02/2014 15:30, Alan Ho wrote:
> 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
My box has "Software" and "Software\Fonts" within HKEY_CURRENT_CONFIG.
But, to give you a slightly different answer, you could try putting
print() calls in at various points so you can understand the flow of the
program if you're not sure it's doing the right thing.
TJG
More information about the Tutor
mailing list