我编写了一个Python脚本,最终应该关闭计算机。
这一行是其中的一部分:
os.system("shutdown /p")
它会进行某种关闭,但仍保留在开启的Windows控制面板上(用户可以切换计算机用户)。
有没有办法完全关闭电脑?
我尝试过其他os.system("shutdown ___")
方法但没有成功。
还有其他可能有帮助的方法吗?
答案 0 :(得分:12)
import os
os.system('shutdown -s')
这对你有用。
答案 1 :(得分:6)
对于Linux:
import os
os.system('sudo shutdown now')
或:如果您希望在没有sudo
提示密码的情况下立即关机,请将以下内容用于Ubuntu和类似的发行版:
os.system('systemctl poweroff')
答案 2 :(得分:2)
对我来说真正没有问题的唯一变体是:
import os
os.system('shutdown /p /f')
答案 3 :(得分:1)
使用ctypes您可以使用ExitWindowsEx功能关闭计算机。
MSDN的说明:
注销交互式用户,关闭系统或关闭并重新启动系统。
首先是一些代码:
import ctypes
user32 = ctypes.WinDLL('user32')
user32.ExitWindowsEx(0x00000008, 0x00000000)
现在逐行说明:
ExitWindowsEx
由user32.dll
提供,需要通过WinDLL()
ExitWindowsEx()
函数并传递必要的参数。<强>参数:强>
所有参数都是十六进制。
我选择的第一个参数:
关闭系统并关闭电源。系统必须支持断电功能。
还有许多其他可能的功能,请参阅the documentation以获取完整列表。
第二个论点:
第二个参数必须给出关闭的原因,该原因由系统记录。在这种情况下,我将其设置为Other issue
,但有很多可供选择。有关完整列表,请参阅this。
跨平台:
这可以与其他方法结合使其跨平台。例如:
import sys
if sys.platform == 'win32':
import ctypes
user32 = ctypes.WinDLL('user32')
user32.ExitWindowsEx(0x00000008, 0x00000000)
else:
import os
os.system('sudo shutdown now')
这是Windows依赖的功能(虽然Linux / Mac会有相同的功能),但是比调用os.system()
更好的解决方案,因为名为shutdown.bat
的批处理脚本不会与命令(并在此期间造成安全隐患)。
此外,如果用"You are about to be signed out in less than a minute"
shutdown -s
之类的消息表明这样做,则不会打扰用户,但会以静默方式执行。
作为旁注,使用subprocess而不是os.system()
(请参阅Difference between subprocess.Popen and os.system)
作为旁注:我构建了WinUtils(仅限Windows),这简化了一点,但它应该更快(并且不需要Ctypes),因为它是用C构建的
示例:强>
import WinUtils
WinUtils.Shutdown(WinUtils.SHTDN_REASON_MINOR_OTHER)
答案 4 :(得分:1)
尝试以下代码段:
import os
shutdown = input("Do you wish to shutdown your computer ? (yes / no): ")
if shutdown == 'no':
exit()
else:
os.system("shutdown /s /t 1")
答案 5 :(得分:0)
此Python代码可以完成任务:
vertical resizing
答案 6 :(得分:0)
以下是关闭 Windows 的示例:
mTabLayout.getTabAt(position).select();
以下是关闭 Linux (通过root权限)的示例:
import os
os.system("shutdown /s /t 1")
答案 7 :(得分:0)
如您所愿,可以使用winapi。
import win32api,win32security,winnt
win32security.AdjustTokenPrivilege(win32security.OpenProcessHandle(win32api.GetCurrentProcess(),win32security.ADJUST_TOKEN_PRIVILEGE | win32security.TOKEN_QUERY),False,[(win32security.LookupPrivilegeValue(None,winnt.SE_SHUTDOWN_NAME),winnt.SE_PRIVILEGE_ENABLE)])
win32api.InitateSystemShutdown(None,"Text",second,Force_Terminate_Apps_as_boolean,Restart_as_boolean)
答案 8 :(得分:0)
pip 安装时间表
如果您还想安排它:
import schedule
import time
import os
when= "20:11"
print("The computer will be shutdown at " + when + "")
def job():
os.system("shutdown /s /t 1")
schedule.every().day.at(when).do(job)
while True:
schedule.run_pending()
time.sleep(1)
答案 9 :(得分:-2)
我想我会展示一个潜在的恶意代码。 所以,现在我将向你们展示代码,请务必明智地使用它,因为如果您考虑一下,这有点恶意。 代码如下:
import os
restart = input("Do you wish to restart your computer ? (yes / no): ")
if restart == 'no':
os.system("shutdown /r /t /1")
else:
os.system("shutdown /r /t 1")
那么,您可能想知道这样的代码有什么恶意?好吧,它并不是真正的恶意,但是,如果您考虑一下,它可能是恶意的。 假设你正在做一个项目,你已经走了这么远,但是…… 你启动任何程序只是为了好玩或其他什么,然后它会问你...... 如果您想重新启动计算机,请记住那个男孩/女孩还没有保存他/她的进度。 所以,基本上它会毁了他/她的整个项目。 所以这可能是恶意的,真的很烦人。 想象一下你打开这个程序,它问你是否要重新启动,你说不,然后它重新启动!!!!!!!!!!!!!!!!! 是的。那。是。烦人。