Скрипт Python для выключения компьютера

Опубликовано: 18 Марта, 2022

As we know, Python is a popular scripting language because of its versatile features. In this article, we will write a Python script to shutdown a computer.
To shut down the computer/PC/laptop by using a Python script, you have to use the os.system() function with the code “shutdown /s /t 1” .

Note: For this to work, you have to import os library in the ide. If you don’t have it, then ‘pip install os‘ through the Command Prompt.

Causion: Please ensure that you save and close all the program before running this code on the IDLE, as the below program will immediately shut down your computer.

Below is the Python implementation –

import os
  
shutdown = input("Do you wish to shutdown your computer ? (yes / no): ")
  
if shutdown == "no":
    exit()
else:
    os.system("shutdown /s /t 1")

Выход:

Вот программа Python, которая попросит пользователя выключить компьютер, предлагая вариант «Да» или «Нет». Кроме того, когда вы набираете «да» и затем нажимаете клавишу ENTER, система будет немедленно выключена.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.