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

2024-09-28 15:37:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在使用Windows10

我希望代码如下所示:

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

这将关闭系统


Tags: to代码命令功能inputifdowhat
2条回答

该功能应在以下情况下发挥作用:

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 /?

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

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

相关问题 更多 >