How to shut down a computer using Python
You are definitely excited about doing some projects with Python when you learn this language. This article will tell you how to shut down a computer using Python. At first, we talk about the required module, then code snippets, and the ideas will be explained. So, stay tuned to W3camps.
OS module
We only need one module, a built-in one, in this code: the OS module. The OS is a standard Python module that provides methods to use operating systems functionality. With the use of the OS module, we could have a lot of interaction with the file system, such as:
- Creating a directory
- Listing out files and directories
- Deleting files and directories
- Reading or writing files
- Shutting down or restarting the system
Note: Make sure to save and close all the current files before shutting down your operating system since you would lose your unsaved data when you run codes.
The video
Shutdown Code Snippet
import os
os.system("shutdown /s /t 1")
On the first line, the OS module is imported so we can use its functions and methods. Then with the use of os.system() it’s possible to shut down the computer. Here /s stands for shutting down. When you run the code given above, your computer gets shut down after default time, i.e., 30 seconds. To shut down the computer immediately, set the timer to 0. The /t stands for timer, and 1 indicates 1 second. Therefore after running this code, the computer gets shut down in1 a second.
Restart Code Snippet
import os
os.system("shutdown /r /t 1")
If you want to restart your computer, all you need to do is to replace /s with /r from the shutdown code snippet. This program will restart your system within 1 second. To shut down or restart the computer immediately, simply replace 1 with 0 to execute the code immediately.
Let the user choose
import os
print("A. Shutdown\nB. Restart\nC. Cancel")
choice = input("What is your choice?")
if choice == 'A':
print("Shutting down...")
os.system("shutdown /s /t 5")
elif choice == 'B':
print("restarting...")
os.system("shutdown /r /t 5")
elif choice == 'C':
print("Canceled!")
exit()
else:
print("Wrong Choice!")
To shut down a computer using Python, the above code could be better because it gives the user more choices and it’s somewhat user-friendly. Let’s explain it:
On lines 3 and 4, we give the user three choices to choose from, and the user could enter one of the ‘A,’ ‘B,’ or ‘C’ characters. One operation could happen in the following code based on the user’s choice. Operations are respectively, “Shut down”, “Restart”, or “Cancel”. Additionally, we consider a situation where the user enters a wrong character, then we show them an appropriate message and exit the code.
Output
Here is what you in your terminal when you run the code:
A. Shutdown
B. Restart
C. Cancel
What is your choice?
All you need to do is enter one of the ‘A,’ ‘B,’ or ‘C’ characters, and the program performs the right actions. Note that your choice is case-sensitive, so use capital letters; otherwise, Python will raise an error. In the following image, you can see the code in the VS code environment:

The application of the code
This code is so practical since if you combine this with a clock project in Python, you could write an application to read the time and turn off or restart your computer at a certain time. To expand this idea, I recommend you to read the “How to create a digital clock with Python” article and write your own application. This could be so exciting. I hope you liked this post. If you have any questions feel free to ask in the comment below.