0

I want a function or a command that will instantly shutdown my laptop.

I am using windows 10.

I want the code to be like:

command = input("what to do? ")
if command == "shutdown":
   shutdown()

That should shut down the system.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

3

Yeah that's easy in case of windows

windows has built in command shutdown /s /t0 for instant shutdown

Code:

import os

def shutdown():
    #shutdown /s -> shuts down the computer [but it takes time]
    #also shows message windows is going to be shutdown within a minute
    #to avoid this we use /t parameter time=0seconds /t0
    #command = shutdown /s /t0
    #execute to the shell
    os.system("shutdown /s /t0")

a = input("What to do?")

if a == "shutdown":
    shutdown()
Pear
  • 351
  • 2
  • 12
0

This function should work:

import subprocess
import shlex
def shutdown():
    subprocess.run(shlex.split("shutdown /s"))

Quoting from https://its.uiowa.edu/support/article/109196:

To shut down your computer, type shutdown /s.
To restart your computer, type shutdown /r.
To log off your computer type shutdown /l.
For a complete list of options type shutdown /?

KetZoomer
  • 2,701
  • 3
  • 15
  • 43