Problem with winreg - The object is not a PyHKEY object

Stephen Hansen apt.shansen at gmail.com
Fri Aug 21 13:22:04 EDT 2009


On Fri, Aug 21, 2009 at 9:33 AM, Jamie <jamie.ivanov at gmail.com> 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
string.

E.g., do:

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
r'SYSTEM\CurrentControlSet\Enum\Root')
_winreg.DeleteKey(key, 'LEGACY_MFEAPFK'
_winreg.CloseKey(key)

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.

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090821/3c002213/attachment-0001.html>


More information about the Python-list mailing list