How to delete the one application's registry key from the regedit using python script?
Asked Answered
D

1

6

I'm new to python. I want to delete the key which is in the regedit using python script.

regedit tree view for my application key

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

In this, I want to delete Test1 key using python script.

I've used below script

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)

Error:

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

can anybody suggest solution for this?

Diplomacy answered 18/10, 2013 at 12:40 Comment(1)
the error seems very clear - the key in registry was not found (most likely because it's not there)Scupper
M
4

Use backslash(\), not forward slash(/). And _winreg.DeleteKey requires at least two argument.

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')
Malacca answered 18/10, 2013 at 12:55 Comment(3)
is it possible to search is "Test1" key is in the registry key or not. if it has, then we should delete the key, else we've throw the error. python script??Diplomacy
@cgsabari, As far as I know, _winreg does not provide search functionality. How about post a new question about that?Malacca
ya new question only. but main thing is deleting the key. before that i felt its better to know its exist or not. so raised the question here. thats it.Diplomacy

© 2022 - 2024 — McMap. All rights reserved.