How To Shutdown Restart or Logoff your Windows Computer using Python Program

Here we have a Python Program to Shutdown restart or Logoff your Windows Computer

You can Shutdown or restart or logoff your windows computer from a python program by using shutdown program present in system32 folder.

we are using the system function available in the os library.

Example Python Program to Shutdown your Computer

1 import os
2 
3 choice = input("Shut Down your Computer ? ( y or n ) ")
4 
5 if choice == 'y' or choice == 'Y':
6     os.system("shutdown /s")
7 else:
8     print("Exiting the program doing nothing")

Example Python Program to Restart your Computer

1 import os
2 
3 choice = input("Restart your Computer ? ( y or n ) ")
4 
5 if choice == 'y' or choice == 'Y':
6     os.system("shutdown /r")
7 else:
8     print("Exiting the program doing nothing")

Example Python Program to Logoff your Computer

1 import os
2 
3 choice = input("Logoff your Computer ? ( y or n ) ")
4 
5 if choice == 'y' or choice == 'Y':
6     os.system("shutdown /l")
7 else:
8     print("Exiting the program doing nothing")

Watch this video to learn how this works