Search code examples
pythonwinreg

Python registry script no output


Hey Im new to python tried executing my registry change code but got No output just getting "Process finished with exit code 0".

import os
import winreg
def usbenordis(value):
    print(value)
    keyval = r"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UsbStor"
    if not os.path.exists(keyval):
        print("creating key")
        key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, keyval)
    registrykey= winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UsbStor", 0,KEY_WRITE)
    print("open key")
    if value == True:
        SetValueEx(registrykey,"start",0,REG_DWORD,4)
        print("usb disabled")
    elif value == False:
        SetValueEx(registrykey,"start",0,REG_DWORD,3)
        print("usb enabled")
    else:
        print("op cancelled")
        winreg.CloseKey(registrykey)
    return True

def main():
    usbenordis(True)

Solution

  • Try changing this:

    if not os.path.exists("keyval"):
    

    to this:

    if not os.path.exists(keyval):
    

    You've defined keyval as a variable but you're then passing the string 'keyval' to the function.

    Also you need to change your boolean values to have uppercase first letters (true = True, false = False)

    EDIT: Are you definitely calling the main() function? Try adding the following to the bottom of your code and running (outside of any functions);

    if __name__ == "__main__":
        main()