Reputation: 47
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)
Upvotes: 0
Views: 249
Reputation: 47
Working soln
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, r"SYSTEM\CurrentControlSet\Services\USBSTOR")
registrykey= winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\CurrentControlSet\Services\USBSTOR", 0,winreg.KEY_ALL_ACCESS)
# print("open key")
if value == "disable":
winreg.SetValueEx(registrykey,"start",0,winreg.REG_DWORD,4)
print("usb disabled")
elif value == "enable":
winreg.SetValueEx(registrykey,"start",0,winreg.REG_DWORD,3)
print("usb enabled")
else:
print("op cancelled")
winreg.CloseKey(registrykey)
return True
def main():
# print("input enable or disable")
X=input("Input enable or disable:")
usbenordis(X)
if __name__== "__main__":
main()
Upvotes: 1
Reputation: 3001
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()
Upvotes: 2