Shutdown and Restart PC using Python
In this tutorial, we are going to learn how to Shutdown and restart the PC using Python. For this firstly, we need to import the OS module in our code. OS is an inbuilt module present in Python. Let’s know something about the OS module.
Shutdown and restart PC using OS Module in Python
The OS module in Python consists of functions that work on Operating Systems like Windows, Mac or Linux. Let us see the program.
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")
In the above program, we have imported the os module and we are entering a value for the ‘a’ variable. If we enter 1 the PC will shut down and if we enter 2 the PC will restart. We are giving a command in the system function present in the OS module.
1 - Shutdown, 2 - Restart
The system will shut down/restart immediately after you enter the input. So ensure that all other applications are closed and any unsaved work is saved.
Also read: os.unlink() method in Python with examples