如何制作一个可以注销、关闭和重新启动计算机的python脚本? [英] How to make a python script which can logoff, shutdown, and restart a computer?
问题描述
背景
我目前正在自学 python,我认为拥有一种控制中心"将是一个非常酷的项目,我可以在其中关闭、重新启动和注销我的计算机.我也想使用 subprocess 模块,因为我听说 import OS 模块已经过时了.
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.
当前代码
def shutdown(self):
import subprocess
subprocess.call(["shutdown", "-f", "-s", "-t", "60"])
问题
我真正想问的是,有没有办法(使用子进程模块)注销并重新启动我的计算机?
What I am really asking is, is there a way (using the subprocess module) to logoff of and restart my computer?
技术规格
Python 2.7.3
Python 2.7.3
Windows 7,32 位
Windows 7, 32 bit
推荐答案
重启:
shutdown /r
要注销:
shutdown /l
最终代码块(根据要求):
The final code block (as requested):
注销:
def shutdown(self):
import subprocess
subprocess.call(["shutdown", "-f", "-s", "-t", "60"])
重启:
def shutdown(self):
import subprocess
subprocess.call(["shutdown", "-f", "-r", "-t", "60"])
这篇关于如何制作一个可以注销、关闭和重新启动计算机的python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!