[Tutor] Recursive functions and the windows registry

Alan Gauld alan.gauld at btinternet.com
Thu Nov 16 23:41:49 CET 2006


"Todd Dahl" <lucidmonk at gmail.com> wrote

> By the way I don't understand why it seems to work inside the
> ListRegistryKeys function but it doesn't pass all the information 
> back to
> the parseRegistry function.

> def ListRegistryKeys(path):
> ...
>    name = []
>
>    try:
>        while 1:
>            name.append(path + "\\" + _winreg.EnumKey(key, i))
>            print name[-1]
>            i += 1
>    except WindowsError:
>        pass

I'd rewrite this with the try/except inside the loop.

while True:  # use the boolean value, its clearer IMHO :-)
    try:
       name.append(...)
       print name[-1]
       i += 1
    except WindowsError: pass

However since you should(?) only get the error when you run out
of keys to iterate that shouldn't cause your problem...

>    for item in name:
>        ListRegistryKeys(item)

Note that this call will create a new name list inside the recursive
call. You probably need to do:

           name += ListRegistryKeys(item)

>    return name

Otherwise this just returns the top level list.

> def parseRegistry():
>    guidlist = ListRegistryKeys("CLSID")
>
>    for item in guidlist:
>        print "parseRegistry: ", item
>
> def main():
>    parseRegistry()
>
> if __name__=="__main__":
>    main()

You could just call parseRegistry directly, there is no rule to
say you need to use a main() function.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list