如何使用 Python 关闭计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34039845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to shutdown a computer using Python
提问by Ben L
I've written a Python script which should eventually shutdown the computer.
我写了一个 Python 脚本,它最终应该关闭计算机。
This line is a part of it :
这一行是其中的一部分:
os.system("shutdown /p")
It makes some sort of a shutdown but remains on the turn-on Windows control pannel (where the user can switch the computer users).
它进行某种关闭,但仍保留在打开的 Windows 控制面板上(用户可以在其中切换计算机用户)。
Is there a way to fully shutdown the computer?
有没有办法完全关闭计算机?
I've tried other os.system("shutdown ___")
methods with no success.
我试过其他os.system("shutdown ___")
方法都没有成功。
Is there another method which might help?
还有另一种方法可能有帮助吗?
回答by shiva1791
import os
os.system('shutdown -s')
This will work for you.
这对你有用。
回答by Mtl Dev
For Linux:
对于 Linux:
import os
os.system('sudo shutdown now')
or: if you want immediate shutdown without sudo
prompt for password, use the following for Ubuntu and similar distro's:
或者:如果您想在不sudo
提示输入密码的情况下立即关闭,请对 Ubuntu 和类似发行版使用以下命令:
os.system('systemctl poweroff')
回答by Simon
Using ctypesyou could use the ExitWindowsExfunction to shutdown the computer.
使用ctypes,您可以使用ExitWindowsEx函数关闭计算机。
Description from MSDN:
来自 MSDN 的描述:
Logs off the interactive user, shuts down the system, or shuts down and restarts the system.
注销交互式用户、关闭系统或关闭并重新启动系统。
First some code:
首先是一些代码:
import ctypes
user32 = ctypes.WinDLL('user32')
user32.ExitWindowsEx(0x00000008, 0x00000000)
Now the explanation line by line:
现在逐行解释:
- Get the ctypes library
- The
ExitWindowsEx
is provided by theuser32.dll
and needs to be loaded viaWinDLL()
- Call the
ExitWindowsEx()
function and pass the necessary parameters.
- 获取 ctypes 库
- 由
ExitWindowsEx
提供user32.dll
,需要通过加载WinDLL()
- 调用
ExitWindowsEx()
函数并传递必要的参数。
Parameters:
参数:
All the arguments are hexadecimals.
所有的参数都是十六进制的。
The first argument I selected:
我选择的第一个参数:
shuts down the system and turns off the power. The system must support the power-off feature.
关闭系统并关闭电源。系统必须支持关机功能。
There are many other possible functions see the documentationfor a complete list.
还有许多其他可能的功能,请参阅文档以获取完整列表。
The second argument:
第二个论点:
The second argument mustgive a reason for the shutdown, which is logged by the system. In this case I set it for Other issue
but there are many to choose from. See thisfor a complete list.
第二个参数必须给出关闭的原因,这是由系统记录的。在这种情况下,我设置了它,Other issue
但有很多可供选择。请参阅此以获取完整列表。
Making it cross platform:
使其跨平台:
This can be combined with other methods to make it cross platform. For example:
这可以与其他方法结合使用以使其跨平台。例如:
import sys
if sys.platform == 'win32':
import ctypes
user32 = ctypes.WinDLL('user32')
user32.ExitWindowsEx(0x00000008, 0x00000000)
else:
import os
os.system('sudo shutdown now')
This is a Windows dependant function (although Linux/Mac will have an equivalent), but is a better solution than calling os.system()
since a batch script called shutdown.bat
will not conflict with the command (and causing a security hazard in the meantime).
这是一个依赖于 Windows 的函数(尽管 Linux/Mac 将有一个等效的函数),但它是比调用更好的解决方案,os.system()
因为调用的批处理脚本shutdown.bat
不会与命令冲突(同时会导致安全隐患)。
In addition it does not bother users with a message saying "You are about to be signed out in less than a minute"
like shutdown -s
does, but executes silently.
此外,它不会用一条消息来打扰用户,"You are about to be signed out in less than a minute"
就像shutdown -s
是一样,而是静默执行。
As a side note use subprocessover os.system()
(see Difference between subprocess.Popen and os.system)
作为一个侧面说明使用子了os.system()
(见subprocess.Popen和使用os.system的区别)
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.
附带说明:我构建了WinUtils(仅限 Windows),它稍微简化了这一点,但是它应该更快(并且不需要 Ctypes),因为它是用 C 构建的。
Example:
例子:
import WinUtils
WinUtils.Shutdown(WinUtils.SHTDN_REASON_MINOR_OTHER)
回答by D. Robusto
This Python code may do the deed:
这个 Python 代码可能会这样做:
import os
os.system('sudo shutdown -h now')
回答by Vladimir
The only variant that really workes for me without any problem is:
唯一真正对我有用而没有任何问题的变体是:
import os
os.system('shutdown /p /f')
回答by Benyamin Jafari
Here's a sample to power off Windows:
这是关闭Windows电源的示例:
import os
os.system("shutdown /s /t 1")
Here's a sample to power off Linux(by root permission):
这是关闭Linux电源的示例(通过 root 权限):
import os
os.system("shutdown now -h")
回答by MANTHAN
Try this code snippet:
试试这个代码片段:
import os
shutdown = input("Do you wish to shutdown your computer ? (yes / no): ")
if shutdown == 'no':
exit()
else:
os.system("shutdown /s /t 1")