Computer Shut Off Python 3.4

*爱你&永不变心* 提交于 2019-12-25 04:07:44

问题


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?

OS: Mac OSX Yosemite

Thanks


回答1:


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")

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…

or

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


来源:https://stackoverflow.com/questions/28438247/computer-shut-off-python-3-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!