Python Program to Shutdown and Restart Computer

Python Program to Shutdown and Restart Computer

  • To shutdown and restart your computer or pc or laptop using python code, you have to first import os library and then use os.system() function with the code "shutdown /s /t 1" and "shutdown /r /t 1" to shutdown and restart your computer in a second.

Code to Shutdown Computer

# Python Program - Shutdown Computer
   
import os
check = input("Want to shutdown your computer ? (y/n): ")
if check == 'n':
    exit()
else:
    os.system("shutdown /s /t 1")

Code to Restart Computer

# Python Program - Restart Computer

import os
check = input("Want to restart your computer ? (y/n): ")
if check == 'n':
    exit()
else:
    os.system("shutdown /r /t 1")


 

 

 

Comments