从 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,没有通用的方法可以做到这一点。
对于 Windows,您需要访问 Win32 API。就像这样:
win32api 模块是 pywin32 的一部分。
对于linux/os x,我想调用“reboot”命令是最简单的。
或者类似的东西。
(反对者请注意:
os.system()
尚未被弃用。文本是“子进程模块提供了更强大的工具来生成新进程并检索其结果;使用该模块比使用此函数更可取。”对于像这样的简单情况,当您对检索结果或多重处理不感兴趣时,os.system()
工作得很好)。There is no generic way of doing this, afaik.
For Windows, you need to access the Win32 API. Like so:
The win32api module is a part of pywin32.
For linux/os x, I guess calling the "reboot" command is the easiest.
Or something like that.
(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).您可以使用以下命令重新启动 Windows 系统:
os.system("shutdown -t 0 -r -f")
示例:
更改 -t 前面的数字可更改关闭前的秒数。
You could reboot a Windows system by using:
os.system("shutdown -t 0 -r -f")
Example:
Change the number in front of -t to change the number of seconds before shutdown.
为什么不直接调用 使用子进程关闭命令?
Why don't you just call the shutdown command using subprocess?
标准库中没有任何内容可以直接允许您执行此操作,并且我不知道任何提供跨平台方法的模块,但是如果您在 Windows 上并且不想安装任何东西(例如win32api) 那么你可以使用 ctypes 默认模块并直接与 WinAPI 交互。
您可以使用
ExitWindowsEx( )
功能来重新启动计算机(它几乎与我的其他答案相同如何使用Python关闭计算机;但是必须更改十六进制值才能重新启动而不是关闭)。流程:
首先您需要 ctypes:
接下来获取
user32.dll
,如 文档:因此:
接下来您需要调用
ExitWindowsEx()
函数并插入正确的十六进制值:第一个参数 (
0x00000002
) 关闭系统,然后重新启动 (请参阅文档)。第二个参数 (
0x00000000
) 给出了系统记录的原因。可以在此处找到完整列表完整代码:
关于 Windows 上的
os.system()
方法:win32api 或 ctypes 答案都将静默执行。 os.system("shutdown -t 0 -r -f") 将留下一条消息,指出“您将在不到一分钟的时间内退出”,这在某些情况下可能是不受欢迎的。
名为
shutdown.bat
/shutdown.exe
/shutdown.cmd
的脚本旁边的文件将导致命令shutdown -t 0 - r -f 来中断,调用该文件而不是系统命令。上面描述的
wmic.exe
也是如此。作为旁注:我构建了WinUtils(仅限Windows),它简化了这有点,但是它应该更快(并且不需要 Ctypes),因为它是用 C 构建的。
示例:
There is nothing in the standard library that would directly allow you to do this, and I am unaware of any modules that provide a cross platform method, but if you are on Windows and don't want to install anything (like the win32api) then you could use ctypes default module and interact with the WinAPI directly.
You could use the
ExitWindowsEx()
function to restart the computer (it is almost the same as my other answer How to shudown a computer using Python; however the hexadecimal value has to be changed to restart and not shutdown).Process:
First you need ctypes:
Next get the
user32.dll
as described in the documentation:So:
Next you need to call the
ExitWindowsEx()
function and insert the correct hexadecimal values:The first argument (
0x00000002
) shuts down the system and then restarts (see documentation).The second argument (
0x00000000
) gives a reason to be logged by the system. A complete list can be found hereComplete Code:
About
os.system()
method on Windows:The win32api or ctypes answers both will execute silently.
os.system("shutdown -t 0 -r -f")
will leave a message saying "You are about to be signed out in less than a minute" which may be undesirable in some cases.A file alongside the script called
shutdown.bat
/shutdown.exe
/shutdown.cmd
will cause the commandshutdown -t 0 -r -f
to break, calling that file and not the system command. The same goes for describedwmic.exe
above.As a side note: I built WinUtils (Windows only) which simplifies this a bit, however it should be faster (and does not require Ctypes) since it is built in C.
Example: