Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

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

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.

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

Windows 7, 32 bit

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you have to:

import subprocess

To shutdown your Windows PC:

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

To restart your windows PC

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

To logout your Windows PC:

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

To shutdown your Windows PC after 900s:

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...