How to shutdown the system using python
Warning--please close all the running application before executing the code because it will immediately shutdown the system ,otherwise your unsaved work might get lost.
Shutdown
# Python Program to Shutdown the Computer
import os
#if the user press y then the system will get shutdown
value = input("Do you Want to shutdown your computer ? (y/n): ")
if value == 'y':
os.system("shutdown /s /t 1")
else:
exit()
Shutting down the system at a particular time
# Python Program to Shutdown the Computer at a given timeimport os
from datetime import datetime
#input the time in 24 hr format if your computer timing is in 24 hr format
#example 16:36 or 10:04
value = input("Input the time : ")
while(True):
#it will fetch the current time
now = datetime.now()
#to extract the time in hour and minutes
current_time = now.strftime("%H:%M")
if value == current_time:
os.system("shutdown /s /t 1")
Restart the System
# Python Program to Shutdown the Computer
import os
#if the user press y then the system will get restart
value = input("Do you Want to shutdown your computer ? (y/n): ")
if value == 'y':
os.system("shutdown /r /t 1")
else:
exit()
Comments
Post a Comment