In this tutorial we will discuss How to shutdown, restart or logout computer using Python script
Python is one of the languages with the most adaptable features. In this tutorial we will see one of these features in Python. We will see in Python code that will help to shutdown, restart, and even logout a computer automatically.
Need: To execute any of the above given tasks using the Python script. First ,we need to import the ‘os library’ in our code. We will do this by the command, ‘pip install os’.
Note: We need to close all the running programs and save them before we run the codes mentioned underneath. These codes will without delay shut down or restart the computer.
1. Shut down the computer using Python
Use command:Â shutdown /s /t 1
Code:
import os
Input = input("Want to shutdown now? (Y/N): ")
if Input == 'N':
exit()
else:
os.system("shutdown /s /t 1")
Output:
Want to shutdown now? (Y/N):
Ater, run the code We will get output on the computer screen where we see an option to select between the two option and we have to input our option. If we input ‘Y’, the computer shutdowns without delay.
2. Restart the computer in Python
Use Commands :Â shutdown /r /t 1
Code:
import os;
Input = input("Want to restart now? (Y/N): ")
if Input == 'N':
exit()
else:
os.system("shutdown /r /t 1")
Output:
Want to restart now? (Y/N):
Then again, after run the code output will show on the computer screen where we will see an option to choose between the two choices and we have to input our option. If we input ‘Y’, the computer restarts.
3. Log out of the computer using Python
Used Command:Â shutdown -l
Code:
import os
Input = input("Want to log out now? (Y/N): ")
if Input == 'N':
exit()
else:
os.system("shutdown -l")
Output:
Want to log out now? (Y/N):
In this last snippet again, We will see like before in above code on PC screen where we get an choice to choose between the two choices and we have to input our option. If we input ‘Y’, the computer will log out right away.
Also Read: find minimum and maximum value in a heterogeneous
0 Comments