简体   繁体   中英

How can I shut down my computer using python script?

I want to shut down my computer(mac) using a python script for some code I have written. I have tried the following

os.system('shutdown -s')
os.system('shutdown -h now')

But both of these produce the output 256 and my system doesn't shut down.

I read it might be due to not having root privileges but I guess I can't provide root privileges through python. Is there a way I can run a python script and shut down my mac with the above problem ?

For restart:

re_start = ["shutdown", "-f", "-r", "-t", "30"]

For shutdown:

 shut_down = ["shutdown", "-f", "-s", "-t", "30"]

Hence:

def shutdown(self):
    import subprocess
    subprocess.call(shut_down)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM