我正在用 python 制作个人语音助手。在我的程序中,我编写了一个可以关闭我的电脑的函数。但我也想在关闭之前关闭所有打开的窗口。那么是否有任何python脚本可以做到这一点或任何Windows快捷键?
答案 0 :(得分:0)
您可以使用子进程执行 powershell
或 DOS shutdown
脚本:
import subprocess
# forced shutdown closes windows without saving; you might need to add shell=True
COMMAND = "Stop-Computer -Force"
subprocess.run(['powershell', '-command', COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
我们也可以关闭windows再关机
# replace command above with below
COMMAND = """(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}; Get-Process | Where-Object {$_.MainWindowTitle -ne \"\"} | stop-process; Stop-Computer"""
DOS
# from shutdown documentation
# /s Shuts down the computer
# /f Forces running applications to close without warning users.
# /t <xxx> Sets the time-out period before shutdown to xxx seconds.
subprocess.run(['shutdown', '/s', '/t 0', '/f'], shell=False)