You can execute powershell
or DOS shutdown
script using subprocess:
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)
We can also close windows and then shutdown
# 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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…