Problem with winreg - The object is not a PyHKEY object

Stephen Hansen apt.shansen at
Fri Aug 21 19:22:04 CEST 2009

On Fri, Aug 21, 2009 at 9:33 AM, Jamie <jamie.ivanov at> wrote:

> My goal is to remotely remove the registry keys for McAfee. I don't
> know how winreg handles an exception if a key doesn't exist, but I
> setup my script to skip the exception. But it doesn't seem to work
> right.. I think the script should be self explanitory, please help!
> Please forgive me, but I'm a python newbie.
> ## SCRIPT ##
> import _winreg
> print "Removing McAfee registry entries"
> hkey = _winreg.ConnectRegistry(r'\
> \000000439140PC',_winreg.HKEY_LOCAL_MACHINE)
> try:
> _winreg.DeleteKey('SYSTEM\CurrentControlSet\Enum\Root','LEGACY_MFEAPFK')
> except:
> pass

You have to "open" the key first-- the first argument to .DeleteKey is not a

E.g., do:

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
_winreg.DeleteKey(key, 'LEGACY_MFEAPFK'

The above is an example off the top of my head, not actually tested-- But
that's sorta your problem, I believe: .DeleteKey operates on a key /handle/
in its first argument, not a string. The second argument is a string.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list