0

Activestate.orgから次のPythonレシピを取得し、キーを削除するメソッドを追加しただけですが、エラー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.')

誰かがそれを手伝うことができますか?

4

1 に答える 1

0

ひょっとして、64 ビットの Windows 7 を実行していませんか? 削除に異なる API を使用する必要がある 32 ビット プログラムと 64 ビット プログラムの両方を実行することを考慮して、レジストリの構造にいくつかの変更がありました。 Win32 API ドキュメントではRegDeleteKeyRegDeleteKeyEx場合によっては使用について言及しています。Win32 API は、Windows のメジャー バージョンごとに確実に使用することは困難です。残念ながら、pywin32多くの頭痛の種を隠すために最善を尽くしていますが、効果的に使用する前に、Win32 API とその警告を本当に知っている必要があります.

于 2012-04-10T12:32:30.937 に答える