How do I shutdown and restart the computer using python
Published February 07, 2022In this python example, we'll write a Python program that shuts down or restarts a computer. We will do the following in our program:
- Turn off/shut down the computer.
- Restart the computer.
Simply follow the instructions below to construct a Python application that restarts or shuts down a computer:
Step 1: First, import all of the necessary libraries. We will use the OS library to restart or shut down a pc. Now, paste the code below onto your IDE.
import os |
Step 2: To restart and shut down the device, we'll use the os.system () function. To shut down the machine, we'll use the os.system ("shutdown/s"), and to restart it, we'll replace/s with/r. Therefore We'll use the os. system ("shutdown/r") to restart the machine. Now copy and paste the following code:
import os a = int(input("1 - Shutdown, 2 - Restart")) if a == 1: os.system("shutdown /s /t 1") elif a == 2: os.system("shutdown /r /t 1") |
Step 3: Run the program now. However, before starting the program, make sure you've saved all of the documents or other data that may be lost, since if your computer restarts or shuts down, you could lose all of your unsaved documents.
Code
![]() |
![]() |
Conclusion: In article we covered shuts down/ restart system programatically in python.
Article Contributed By :
|
|
|
|
260 Views |