Как выключить компьютер с помощью python
Перейти к содержимому

Как выключить компьютер с помощью python

  • автор:

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.

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.

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.

How to shutdown a computer using Python

I’ve written a Python script which should eventually shutdown the computer.

This line is a part of it :

It makes some sort of a shutdown but remains on the turn-on Windows control pannel (where the user can switch the computer users).

Is there a way to fully shutdown the computer?

I’ve tried other os.system(«shutdown ___») methods with no success.

Is there another method which might help?

10 Answers 10

This will work for you.

shiva1791's user avatar

or: if you want immediate shutdown without sudo prompt for password, use the following for Ubuntu and similar distro’s:

Xantium's user avatar

Mtl Dev's user avatar

Using ctypes you could use the ExitWindowsEx function to shutdown the computer.

Description from MSDN:

Logs off the interactive user, shuts down the system, or shuts down and restarts the system.

First some code:

Now the explanation line by line:

  • Get the ctypes library
  • The ExitWindowsEx is provided by the user32.dll and needs to be loaded via WinDLL()
  • Call the ExitWindowsEx() function and pass the necessary parameters.

Parameters:

All the arguments are hexadecimals.

The first argument I selected:

shuts down the system and turns off the power. The system must support the power-off feature.

There are many other possible functions see the documentation for a complete list.

The second argument:

The second argument must give a reason for the shutdown, which is logged by the system. In this case I set it for Other issue but there are many to choose from. See this for a complete list.

Making it cross platform:

This can be combined with other methods to make it cross platform. For example:

This is a Windows dependant function (although Linux/Mac will have an equivalent), but is a better solution than calling os.system() since a batch script called shutdown.bat will not conflict with the command (and causing a security hazard in the meantime).

In addition it does not bother users with a message saying «You are about to be signed out in less than a minute» like shutdown -s does, but executes silently.

As a side note: I built WinUtils (Windows only) which simplifies this a bit, however it should be faster (and does not require Ctypes) since it is built in C.

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:

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:

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:

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:

Here is the initial output produced by this python program:

python shutdown computer

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:

Restart Computer Immediately

This python program restarts your computer in 0 second or immediately:

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:

with the statement given below:

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:

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:

python restart computer

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:

python shutdown and restart computer

Now enter the input say 180 to shutdown the system after 180 seconds or 3 minutes.

Shutdown Computer using Python

The OS module in Python is used in various tasks that depend on the operating system. You must have used it before when writing or reading files from your computer to your Python program. It can also be used to shutdown your computer in just a few lines of code. If you want to learn more about shutting down your system using the OS module, then this article is for you. In this article, I will present a tutorial on how to shutdown your computer using Python.

Shutdown Computer using Python

To shutdown your system using the Python programming language, you must have some knowledge of the OS module in Python. It comes preinstalled in the Python standard library, so you don’t need to write a pip command to install it on your Python environment. From reading or writing a file to shut down your system using Python, the OS module can be used in any task that depends on your system’s operating system.

To shutdown your computer with Python, be sure to save and close all running files. Now here’s how to shutdown your computer using Python:

As mentioned above, make sure you have saved and closed all files except your code editor where you wrote the Python program to shutdown your system. As soon as your program starts running, your system will shut down in the next few seconds.

Summary

So this is how you can easily shutdown your system by using the Python programming language. The OS module in Python is a very useful tool as it can be used in many more tasks that are completely dependent on your system’s operating system. I hope you liked this article on how to shutdown your computer using Python. Feel free to ask your valuable questions in the comments section below.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *