计算机关闭Python 3.4 [英] Computer Shut Off Python 3.4

查看:107
本文介绍了计算机关闭Python 3.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友想使用Python 3.4自动关闭计算机(放入程序中).有人知道这样做的基本方法吗?

A friend of mine wants to automatically shut off his computer using Python 3.4 (to put in a program). Does anyone know a basic as-small-as-possible way to do this?

操作系统: Mac OSX Yosemite

OS: Mac OSX Yosemite

谢谢

推荐答案

OS X在其基础上是Unix,因此您可以使用Unix命令.您可以使用:

OS X is a Unix at its base, so you can use Unix commands. you can use either:

#!/usr/bin/python
import os
os.system("shutdown -h now")

必须以root身份运行,否则将无法工作.但是,对于想要执行脚本并使用sudo shutdown …而不是shutdown…

Must be run as root, won't work otherwise. You can however add to sudoers (man visudo) shutdown as NOPASSWD program for the users that want to execute the script and use sudo shutdown … instead of just shutdown…

import subprocess
subprocess.call(['osascript', '-e',
'tell app "System Events" to shut down'])

这篇关于计算机关闭Python 3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆