首页 > 解决方案 > Python 中是否有用于在 Windows 10 上即时关闭的命令?

问题描述

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

我正在使用 Windows 10。

我希望代码如下:

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

那应该关闭系统。

标签: pythonpython-3.xwindowsshutdown

解决方案


是的,在窗户的情况下这很容易

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

推荐阅读