Python 中是否有用于在 Windows 10 上立即关闭的命令?

Is there a command in Python for instant shutdown on Windows 10?

我想要一个可以立即关闭笔记本电脑的功能或命令。

我正在使用 windows10.

我希望代码如下:

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

这应该会关闭系统。

是的,在 windows

的情况下这很容易

windows 已内置即时关机命令 shutdown /s /t0

代码:

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()

这个函数应该可以工作:

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

引用自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 /?