【问题标题】:How to shutdown a computer using Python如何使用 Python 关闭计算机
【发布时间】:2016-03-06 12:33:37
【问题描述】:

我编写了一个 Python 脚本,它最终应该会关闭计算机。

这条线是它的一部分:

os.system("shutdown /p")

它会进行某种关闭,但仍保留在打开的 Windows 控制面板上(用户可以在其中切换计算机用户)。

有没有办法完全关闭计算机?

我尝试了其他 os.system("shutdown ___") 方法,但没有成功。

还有其他方法可能有帮助吗?

【问题讨论】:

  • 这实际上与 Python 没有任何关系,因为 shutdown /p 只是作为终端命令发送到系统。请指定您的操作系统版本,因为选项可能会有所不同。
  • 当然可以。但我的问题是是否有办法使用 Python 模块?或其他方式?而且我需要在多台电脑上运行程序,所以操作系统版本不一样。谢谢

标签: python windows cmd shutdown


【解决方案1】:
import os
os.system('shutdown -s')

这对你有用。

【讨论】:

  • 其中一个问题是它会留下一条消息说"You are about to be signed out in less than a minute"。另一个是,如果有人将名称为shutdown.bat 的批处理脚本与该脚本一起放置,它将无法工作,这也是一个安全隐患,因为该文件将被执行。 Subprocess 现在应该在 os.system() 上使用。
  • 您可以避免该消息 - os.system('shutdown -s -t 0')
【解决方案2】:

对于 Linux:

import os
os.system('sudo shutdown now')

或者:如果您想在没有sudo 提示输入密码的情况下立即关机,请在 Ubuntu 和类似发行版中使用以下命令:

os.system('systemctl poweroff') 

【讨论】:

  • Subprocess 现在应该在 os.system() 上使用。
【解决方案3】:

使用ctypes 可以使用ExitWindowsEx 函数关闭计算机。

来自 MSDN 的说明:

注销交互式用户,关闭系统,或关闭并重新启动系统。


首先是一些代码:

import ctypes

user32 = ctypes.WinDLL('user32')

user32.ExitWindowsEx(0x00000008, 0x00000000)

现在逐行解释:

  • 获取 ctypes 库
  • ExitWindowsExuser32.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 的批处理脚本不会与命令冲突(并导致同时存在安全隐患)。

此外,它不会像shutdown -s 那样用"You are about to be signed out in less than a minute" 的消息打扰用户,而是静默执行。

作为旁注使用subprocess 而不是os.system()(请参阅Difference between subprocess.Popen and os.system


附带说明:我构建了 WinUtils(仅限 Windows),这稍微简化了一点,但是它应该更快(并且不需要 Ctypes),因为它是用 C 构建的。

示例:

import WinUtils
WinUtils.Shutdown(WinUtils.SHTDN_REASON_MINOR_OTHER)

【讨论】:

  • 我更喜欢 poweroff 或 init 6
【解决方案4】:

唯一真正对我有用且没有任何问题的变体是:

import os
os.system('shutdown /p /f')

【讨论】:

    【解决方案5】:

    Python doc 建议使用 subprocess 而不是 os.system。它打算替换 os.system 等旧模块。

    所以,使用这个:

    import subprocess
    subprocess.run(["shutdown", "-s"])
    

    对于linux用户-s不是必需的,他们可以使用

    import subprocess
    subprocess.run(["shutdown"])
    

    【讨论】:

      【解决方案6】:

      试试这个代码 sn-p:

      import os 
      
      shutdown = input("Do you wish to shutdown your computer ? (yes / no): ") 
      
      if shutdown == 'no': 
          exit() 
      else: 
          os.system("shutdown /s /t 1") 
      

      【讨论】:

        【解决方案7】:

        这个 Python 代码可以做到这一点:

        import os
        os.system('sudo shutdown -h now')
        

        【讨论】:

          【解决方案8】:

          这是一个关闭Windows的示例:

          import os
          
          os.system("shutdown /s /t 1")
          

          这是一个关闭 Linux 电源的示例(通过 root 权限):

          import os
          
          os.system("shutdown now -h")
          

          【讨论】:

            【解决方案9】:

            如你所愿,可以使用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)
            

            【讨论】:

              【解决方案10】:

              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)
              

              【讨论】:

                猜你喜欢
                • 2014-05-25
                • 2019-11-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-07-13
                • 1970-01-01
                • 2021-07-07
                • 2012-08-31
                相关资源
                最近更新 更多