如何制作一个可以注销、关闭和重新启动计算机的python脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14764126/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:21:50  来源:igfitidea点击:

How to make a python script which can logoff, shutdown, and restart a computer?

pythonwindowssubprocess

提问by xxmbabanexx

Background

背景

I am currently in the process of teaching myself python, and I thought that it would be a very cool project to have a sort of "control center" in which I could shutdown, restart, and log off of my computer. I also want to use the subprocess module, as I have heard that the import OS module is outdated.

我目前正在自学 python,我认为拥有一种“控制中心”将是一个非常酷的项目,我可以在其中关闭、重新启动和注销我的计算机。我也想使用 subprocess 模块,因为我听说 import OS 模块已经过时了。

Current Code

当前代码

def shutdown(self):
    import subprocess
    subprocess.call(["shutdown", "-f", "-s", "-t", "60"])

Question

What I am really asking is, is there a way (using the subprocess module) to logoff of and restart my computer?

我真正要问的是,有没有办法(使用子进程模块)注销并重新启动我的计算机?

Tech Specs

技术规格

Python 2.7.3

蟒蛇 2.7.3

Windows 7, 32 bit

Windows 7,32 位

采纳答案by ben_frankly

To restart:

重启:

shutdown /r

To log off:

要注销:

shutdown /l

The final code block (as requested):

最终代码块(根据要求):

Log off:

注销:

def shutdown(self):
    import subprocess
    subprocess.call(["shutdown", "-f", "-s", "-t", "60"])

Restart:

重新开始:

def shutdown(self):
    import subprocess
    subprocess.call(["shutdown", "-f", "-r", "-t", "60"])

回答by abarnert

If you can't get shutdownto work somehow, you can always just call the function it calls out of the USER library. You coulddo this via ctypesor win32api, but you can also just do this:

如果您无法shutdown以某种方式开始工作,您可以随时调用它从 USER 库中调用的函数。您可以通过ctypes执行此操作win32api,但您也可以这样做:

subprocess.call(['rundll32', 'user.exe,ExitWindowsExec')

Or you could call the higher-level shell function that the start menu uses:

或者您可以调用开始菜单使用的更高级别的 shell 函数:

subprocess.call(['rundll32', 'shell32.dll,SHExitWindowsEx 2')

(See MSDN documentation on these functions.)

(请参阅有关这些功能的 MSDN 文档。)

I think this is probably the worst way to do it. If you want to run a command, run shutdown; if you want to use the API, use win32api. But if you've got a bizarrely screwed-up system where shutdown /rjust doesn't work, it's an option.

我认为这可能是最糟糕的方法。如果要运行命令,请运行shutdown;如果要使用 API,请使用win32api. 但是,如果您有一个奇怪的搞砸了的系统而shutdown /r无法正常工作,那么这是一个选择。

回答by Johan

First you have to:

首先你必须:

import subprocess

To shutdown your Windows PC:

要关闭 Windows PC:

subprocess.call(["shutdown", "/s"])

To restart your windows PC

重新启动 Windows PC

subprocess.call(["shutdown", "/r"])

To logout your Windows PC:

要注销您的 Windows PC:

subprocess.call(["shutdown", "/l "])

To shutdown your Windows PC after 900s:

要在 900 秒后关闭 Windows PC:

subprocess.call(["shutdown", "/s", "/t", "900"])

To abort shutting down because there is no good reason to shutdown your pc with a python script, you were just copy-pasting code from stackoverflow:

要中止关闭,因为没有充分的理由使用 python 脚本关闭您的电脑,您只是从 stackoverflow 复制粘贴代码:

subprocess.call(["shutdown", "/a "])

I only tried these function calls in Python 3.5. First of all, I do not think this has changed since python 2.7, and second: it is 2016, so I guess you have made the switch already since asking this question.

我只在 Python 3.5 中尝试过这些函数调用。首先,我认为自 python 2.7 以来这没有改变,其次:现在是 2016 年,所以我猜你自从问这个问题后已经进行了转换。