pythonpython-2.7winreg

Access is denied in python


I am wanting to add a registry key with python via this code:

import _winreg
from time import sleep
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Run',_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(key,'Windows-Update',0,_winreg.REG_BINARY,'C:\Windows\System32\SystemSetting\Block.exe') 
key.Close()

But it shows up this error WindowsError: [Error 5] Access is denied.

Any solution?

EDIT - I have already run it as admin

EDIT2 - Is it related to KEY_ALL_ACCESS


Solution

  • Run the python program inside command prompt. There is a command prompt (Admin) program available in windows. Or simply right click on Command prompt and select Run as administrator. Ref