2013-10-18 43 views
6

我是python新手。我想使用python脚本删除注册表中的密钥。如何使用python脚本从注册表中删除一个应用程序的注册表项?

注册表编辑器树视图我的应用程序键

HKEY_CURRENT_USER 
| 
|_Software 
     | 
     |_Applications 
        | 
        |_Application 
          |_Test1 
          |_Test2 

在此,我想删除使用python脚本 Test1的关键。

我用下面的脚本

import _winreg 
Key_Name=r'Software/Applications/Application/Test1' 
Key=_winreg.OpenKey(_winreg.HKEY_CURRENT_USER, Key_Name, 0, _winreg.KEY_ALL_ACCESS) 
_winreg.DeleteKey(key) 

错误:

Traceback (most recent call last): 
    File "C:\Users\Test\workspace\Test\DeletePreferences.py", line 9, in <module> 
    key=_winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'Software/Applications/Application/Test1', 0, _winreg.KEY_ALL_ACCESS) 
WindowsError: [Error 2] The system cannot find the file specified 

任何人可以提出该解决方案?

+0

错误似乎很清楚 - 在注册表中的键没有被发现(最有可能的,因为它不存在) –

回答

3

使用反斜杠(\),而不是正斜杠(/)。而_winreg.DeleteKey至少需要两个参数。

import _winreg 
Key_Name = r'Software\Qube Cinema\QubeMaster Pro' 
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, Key_Name, 0, _winreg.KEY_ALL_ACCESS) 
_winreg.DeleteKey(key, 'Test1') 
+0

谢谢你的人...它的工作.. – cgsabari

+0

是有可能被搜索“测试1”键在注册表项中或没有。如果有,那么我们应该删除密钥,否则我们会抛出错误。 python脚本? – cgsabari

+0

@cgsabari,据我所知,[_winreg](http://docs.python.org/2/library/_winreg.html)不提供搜索功能。如何发布有关该问题的新问题? – falsetru