WindowsError: [Error 5] Access is denied With _winreg.enum

kyosohma at gmail.com kyosohma at gmail.com
Wed Nov 28 17:30:43 CET 2007


On Nov 28, 10:04 am, black_13 <jjosb... at gmail.com> wrote:
> I have included a small script the reproduces the error I am having in
> larger script.
> The line 'hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)'
> seems
> to be causing the error but im not sure why.
> --------------------- script ----------------
>
> import _winreg
> import string
>
> def reproduce_error():
>
>     for index in
> xrange(_winreg.QueryInfoKey(_winreg.HKEY_LOCAL_MACHINE)[0]):
>                #get names
>                name =
> _winreg.EnumKey(_winreg.HKEY_LOCAL_MACHINE,index)
>                hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
>                print name
>
>     hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SOFTWARE')
>     print hkey
>     hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,'SAM')
>     print hkey
>
> if __name__ == '__main__':
>     reproduce_error()
> ------------------- end script
> -----------------------------------------
> HARDWARE
> SAM
> Traceback (most recent call last):
>   File "winreg_error.py", line 19, in <module>
>     reproduce_error()
>   File "winreg_error.py", line 10, in reproduce_error
>     hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,name)
> WindowsError: [Error 5] Access is denied

If you move the "print name" line up one line, you'll notice that it's
choking on the Security key. Go to Start --> Run  and type regedit. Go
to that key and right-click it and check its permissions.

On my PC the only user that has complete control is the SYSTEM. So
you'll probably just want to put an exception block in your function
that records the keys that you can't open and continues to run.

Mike



More information about the Python-list mailing list