How to make a Windows 10 computer go to sleep with a python script?

你离开我真会死。 提交于 2019-11-30 16:42:42

You can use

import os
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

If this command doesn't work for you (it actually puts your comp. in hibernation mode) you will need to turn hibernation off:

Powercfg -H OFF

To turn hibernation mode back on:

Powercfg -H ON

Alternatively, you can also use:

Rundll32.exe Powrprof.dll,SetSuspendState Sleep

If you don't want to turn hibernation off just to run a command, you can use psshutdown as an alternative

C:\Windows\System32\psshutdown.exe -d -t 0 

As for what os.system does, straight from the documentation:

Execute the command (a string) in a subshell.

An additional note:

You will need to have administrator permissions to run Powercfg -H *

rundll32.exe powrprof.dll,SetSuspendState 0,1,0 

should work. Note 0,1,0 - that set is for sleep. Without - hibernation.

set hdd close wait time = 0 in power options

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