从python重新启动本地计算机 [英] restart local computer from python

查看:90
本文介绍了从python重新启动本地计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让计算机在重启时运行 Python 程序?通用解决方案很好,但特别是我在 Windows 上.

Is there any way to make the computer a Python program is running on restart? Generic solution is good, but in particular I'm on windows.

推荐答案

没有通用的方法可以做到这一点,afaik.

There is no generic way of doing this, afaik.

对于 Windows,您需要访问 Win32 API.像这样:

For Windows, you need to access the Win32 API. Like so:

  import win32api
  win32api.InitiateSystemShutdown()

win32api 模块是 pywin32 的一部分.

The win32api module is a part of pywin32.

对于 linux/os x,我想调用rebo​​ot"命令是最简单的.

For linux/os x, I guess calling the "reboot" command is the easiest.

import os
os.system('reboot now')

或者类似的东西.

(给downvoters 的注意:os.system() not 已被弃用.文字是子进程模块提供了更强大的工具来生成新进程并检索它们的结果;使用该模块比使用此函数更可取."对于像这样的简单情况,当您对检索结果或多处理不感兴趣时​​,os.system() 工作得很好).

(Note to downvoters: os.system() has not been deprecated. The text is "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." For simple cases like this, when you aren't interested in retrieving the results, nor in multiprocessing, os.system() works just fine).

这篇关于从python重新启动本地计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆