- Python Basic Programs
- Python Program Examples
- Python Print Hello World
- Python Get Input from User
- Python Add Two Numbers
- Add Subtract Multiply Divide
- Python Check Even or Odd
- Python Check Prime or Not
- Python Check Alphabet or Not
- Python Check Vowel or Not
- Python Check Leap Year or Not
- Check Reverse equal Original
- Check Positive Negative Zero
- Python Check Armstrong or Not
- Python Check Palindrome or Not
- Python Check Perfect Number
- Python Find Reverse of Number
- Python Count Digits in Number
- Python Add Digits of Number
- Sum of First and Last Digits
- Python Product of Mid Digits
- Sum of Squares of Digits
- Interchange Digits of Number
- Python Sum of n Numbers
- Python Print ASCII Values
- Python Swap Two Numbers
- Python Swap Two Variables
- Python Fahrenheit to Celsius
- Python Celsius to Fahrenheit
- Python Display Calendar
- Python Days into Years, Weeks
- Find Largest of Two Number
- Find Largest of Three Number
- Python Print Fibonacci Series
- Generate Armstrong Numbers
- Python Make Simple Calculator
- Python Add Binary Numbers
- Binary Number Multiplication
- Python Mathematical Programs
- Find Sum of Natural Numbers
- Find Average of n Numbers
- Python Print Multiplication Table
- Print Table using Recursion
- Python Find Average Percentage
- Python Find Grade of Student
- Find Square Root of Number
- Python Print Prime Numbers
- Find Numbers Divisible by
- Python Find Factors of Number
- Python Find Factorial of a Number
- Python Find HCF & LCM
- Python Kilometres to Miles
- Python Find Area of Square
- Python Find Area of Rectangle
- Python Find Area of Triangle
- Python Find Area of Circle
- Python Find Perimeter of Square
- Find Perimeter of Rectangle
- Python Find Perimeter of Triangle
- Find Circumference of Circle
- Python Simple Interest
- Python Solve Quadratic Equation
- Python Different Set of Operations
- Python Display Powers of 2
- Python Find nCr & nPr
- Python Pattern Programs
- Python Print Pattern Programs
- Python Print Diamond Pattern
- Python Print Floyd's Triangle
- Python Print Pascal's Triangle
- Python List Programs
- Python Count Even/Odd in List
- Python Positive/Negative in List
- Python Even Numbers in List
- Python Odd Numbers in List
- Python Sum of Elements in List
- Sum of Odd/Even Numbers
- Python Element at Even Position
- Python Element at Odd Position
- Python Search Element in List
- Python Largest Number in List
- Python Smallest Number in List
- Python Second Largest in List
- Python Second Smallest in List
- Python Insert Element in List
- Python Delete Element from List
- Python Multiply Numbers in List
- Swap Two Elements in List
- Python 1D Array Program
- Python Linear Search
- Python Binary Search
- Python Insertion Sort
- Python Bubble Sort
- Python Selection Sort
- Remove Duplicates from List
- Python Reverse a List
- Python Merge Two List
- Python Copy a List
- Python Conversion Programs
- Python Decimal to Binary
- Python Decimal to Octal
- Python Decimal to Hexadecimal
- Python Binary to Decimal
- Python Binary to Octal
- Python Binary to Hexadecimal
- Python Octal to Decimal
- Python Octal to Binary
- Python Octal to Hexadecimal
- Python Hexadecimal to Decimal
- Python Hexadecimal to Binary
- Python Hexadecimal to Octal
- Python Matrix Programs
- Python Add Two Matrices
- Python Subtract Two Matrices
- Python Transpose Matrix
- Python Multiply Matrices
- Python String Programs
- Python Print String
- Python Find Length of String
- Python Compare Two Strings
- Python Copy String
- Python Concatenate String
- Python Reverse a String
- Python Swap Two Strings
- Python Uppercase to Lowercase
- Python Lowercase to Uppercase
- Python Check Substring in String
- Python Count Character in String
- Count Repeated Characters
- Python Count Word in Sentence
- Python Count Each Vowels
- Python Capitalize Character
- Python Capitalize Word in String
- Python Smallest/Largest Word
- Remove Spaces from String
- Remove Duplicate Character
- Remove Vowels from String
- Remove Punctuation from String
- Python Remove Word in String
- Python Remove Duplicate Words
- WhiteSpace to Hyphens
- Replace Vowels with Character
- Replace Character in String
- Python Sort String in Alphabetical
- Sort Word in Alphabetical Order
- Extract Number from String
- Python Check Anagram Strings
- Python File Programs
- Python Read a File
- Python Write to File
- Python Append Text to File
- Python Copy Files
- Python Merge Two Files
- Python Counts Characters in File
- Python Count Words in File
- Python File Content in Reverse
- Python Lines Contains String
- Python Delete Line from File
- Python Capitalize Word in File
- Python Replace Text in File
- Replace Specific Line in File
- Python Find Size of File
- Python List Files in Directory
- Python Delete Files
- Python Misc Programs
- Python Reverse a Tuple
- Python Merge Two Dictionary
- Python bytes to String
- Python bytearray to String
- Generate Random Numbers
- Python Print Address of Variable
- Python Print Date and Time
- Python Get IP Address
- Python Shutdown/Restart PC
- Python Tutorial
- Python Tutorial
Python Program to Shutdown and Restart Computer
In this article, you will learn and get code in Python, to shutdown or restart the computer system. Here are the list program programs available over here:
- Shutdown Computer
- Shutdown Computer Immediately
- Shutdown Computer in Given Time by User
- Restart Computer
- Restart Computer Immediately
- Restart Computer in Given Time by User
- Shutdown/Restart Computer based on User's Choice
- Shutdown Linux Based System
Note - All python programs (on Shutdown/Restart) given in this article, are well-tested and executed. Therefore be sure to save all documents before executing any program given here.
Caution - Make sure to save and close all documents before executing any program given below. Because after executing these codes (programs), your system gets shutdown/restart and you can lost your unsaved documents.
To shutdown or restart your computer system (PC or laptop) using a python code, you have to first import the os library and then use os.system() in this way:
os.system("shutdown /s")
to shutdown the system. Replace /s with /r to restart the system. Now let's create the program in Python to do the task.
Shutdown Computer using Python
This python program shutdown your computer system after default time, that is 30 seconds. As soon as you run the program given below, your computer gets shutdown after 30 seconds:
import os os.system("shutdown /s")
Note - Close all documents before executing this program.
Note - The os module provides functions to interact with Operating System.
Note - The os.system() method is used to execute a command in a subshell. Here, the command is of string type.
Shutdown Computer Immediately
To shutdown the system immediately, provide the timer as 0 second. So that the system gets shutdown within 0 second or immediately as shown in this program:
import os os.system("shutdown /s /t 0")
Note - The /t 0 is extra code added in this program (than previous one). Here /t stands for timer and 0 indicates 0 second. Therefore after executing this program, system gets shutdown within 0 second.
Shutdown Computer in Given Time
This program asks from user to enter the number of seconds to set the timer to shutdown the system:
import os print("Enter Number of Seconds to Shutdown System: ") sec = int(input()) strOne = "shutdown /s /t " strTwo = str(sec) str = strOne+strTwo os.system(str)
Here is the initial output produced by this python program:
Now enter the number of seconds say 10 to shutdown the system in 10 seconds.
Note - The str(sec) is used to convert sec (an integer type value) to string. Because to use + operator to concatenate two string values. That is, strOne and strTwo. The second string is the timer value entered by user.
Restart Computer using Python
To restart your computer system using a python program, just replace the /s with /r from the program given to shutdown the system. Rest of the things will be same as shown in the program given below:
import os; os.system("shutdown /r")
Restart Computer Immediately
This python program restarts your computer in 0 second or immediately:
import os os.system("shutdown /r /t 0")
Restart Computer in Given Time
This program is same as the program given in shutdown computer in given time section. The only thing to do is, replace the following statement:
strOne = "shutdown /s /t "
with the statement given below:
strOne = "shutdown /r /t "
Note - Just a matter of s and r, the whole program gets changed.
Note - Also replace the Shutdown word to Restart, from print message.
Shutdown & Restart based on User's Choice
Here is the combined version of both programs, that is shutdown and restart program in python.
This python program asks from user to enter the choice to perform the desired action. That is, choices are given to perform the things such as shutdown immediately, shutdown in given time, and so on as shown in the program given below:
import os print("1. Shutdown Computer Immediately") print("2. Shutdown Computer after Given Time") print("3. Restart Computer Immediately") print("4. Restart Computer after Given Time") print("5. Exit") print(end="Enter Your Choice: ") choice = int(input()) if choice==1: os.system("shutdown /s /t 0") elif choice==2: print(end="Enter Number of Seconds: ") sec = int(input()) strOne = "shutdown /s /t " strTwo = str(sec) str = strOne+strTwo os.system(str) elif choice==3: os.system("shutdown /r /t 0") elif choice==4: print(end="Enter Number of Seconds: ") sec = int(input()) strOne = "shutdown /r /t " strTwo = str(sec) str = strOne+strTwo os.system(str) elif choice==5: exit() else: print("Wrong Choice!")
Here is the snapshot of the above python program, giving user, five options to proceed, what he/she wants to do. That is, either shutdown, restart or exit from the program:
If user enters 1 or 3 as choice, then the computer gets shutdown/restart just after pressing the ENTER
key.
And when user enters 2 as choice, then the program further asks to enter the number of seconds to shutdown the system:
Now enter the input say 180 to shutdown the system after 180 seconds or 3 minutes.
Shutdown Linux Based System
To shutdown linux based system using a Python program, here is the code:
import os os.system("sudo shutdown now")
This program shutdown the linux based system immediately.
Same Program in Other Languages
- Java Shutdown and Restart Computer
- C Shutdown and Restart Computer
- C++ Shutdown and Restart Computer
« Previous Program Python Tutorial »