2012-04-10 509 views
0

我从Activestate.org采取了以下python配方,然后我只是添加了删除密钥的方法,但是我得到错误5,访问被拒绝,并且它只是一个假钥匙刚刚创建来试用该功能。这里的代码蟒蛇RegDeleteKey错误5访问被拒绝

## {{{ http://code.activestate.com/recipes/576860/ (r2) 
import win32api 
import win32con 

def regquerysubkeys(handle, key, keylist=[]): 

#get registry handle 
    reghandle = win32api.RegOpenKeyEx(handle, key, 0, win32con.KEY_ALL_ACCESS)  
    try: 
     i = 0 
    #enumerates subkeys and recursively calls this function again 
     while True: 
      subkey = win32api.RegEnumKey(reghandle, i) 
      #the following is the line I added myself 
      win32api.RegDeleteKey(handle, key) 


      i += 1 
     #braintwister here ;-) 
      regquerysubkeys(handle, key + subkey + "\\", keylist) 
    except win32api.error as ex: 
     #If no more subkeys can be found, we can append ourself 
     if ex[0] == 259: 
      keylist.append(key) 
     #unexpected exception is raised 
     else: 
      raise 
    finally: 
    #do some cleanup and close the handle 
     win32api.RegCloseKey(reghandle) 
#returns the generated list 
    print keylist 

#call to the function 
regquerysubkeys(win32con.HKEY_LOCAL_MACHINE, "SOFTWARE\\suga\\") 

这些是我在控制台中得到的错误。

Traceback (most recent call last): 
File "C:\EclipseWorkspaces\csse120\MMS-auto\test1.py", line 34, in <module> 
regquerysubkeys(win32con.HKEY_LOCAL_MACHINE, "SOFTWARE\\suga\\") 
File "C:\EclipseWorkspaces\csse120\MMS-auto\test1.py", line 14, in regquerysubkeys 
win32api.RegDeleteKey(handle, key) 
pywintypes.error: (5, 'RegDeleteKey', 'Access is denied.') 

任何人都可以帮忙吗?

回答

0

您是否正在运行64位Windows 7?注册表结构有一些变化,以说明运行32位和64位程序,这些程序要求您使用不同的API进行删除。 The RegDeleteKey Win32 API documentation在某些情况下提及使用RegDeleteKeyEx。 Win32 API很难从一个主要版本的Windows可靠地使用到下一个。不幸的是,pywin32尽最大努力隐藏了一些令人头疼的问题,但仍然需要您真正了解Win32 API及其注意事项,然后才能有效使用它。

+0

我已经阅读过,在文档中,但是我正在运行windowXp 32bit,因此我虽然不应该有这样的问题。 – nassio 2012-04-10 12:45:23

+0

奇怪,但关闭抗病毒软件解决了我的情况同样的问题。 – 2015-03-18 15:59:05