2012-04-10 80 views
0

私はActivestate.orgから次のPythonレシピを受け取りました。その後、単にキーを削除するメソッドを追加しましたが、エラー5が発生しました。アクセスが拒否されました。この機能を試してみました。ここにコードがありますpython 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をお使いになりましたか?異なるAPIを使用して削除する必要がある32ビットと64ビットの両方のプログラムを実行するために、レジストリの構造にいくつかの変更がありました。 The RegDeleteKey Win32 API documentationには、RegDeleteKeyExを使用する場合があります。 Win32 APIは、あるメジャーバージョンのWindowsから次のバージョンに確実に使用することは困難です。残念ながら、pywin32はいくつかの頭痛を隠すのに最善を尽くしますが、効果的に使用するには、Win32 APIとその警告を実際に知っておく必要があります。

+0

私はドキュメントでそれを読んだのですが、私はwindowXp 32bitを実行しています。したがって、私はそのような問題はないはずです。 – nassio

+0

奇妙ですが、私の場合、抗ウイルスソフトウェアをオフにして同じ問題を修正してください。 –

関連する問題