OS Reboot, Shutdown, Hibernate, Sleep, Wakeup (Windows Python)
I'm l开发者_如何学Pythonooking for an automatize way of doing Windows Power Management functions: - Reboot - Shutdown - Hibernate - Sleep - Wakeup
Is there a Python module to cover this functionality? Of course any other solutions are also appreciated...
I also went with the command line:
import os
os.system(r'%windir%\system32\rundll32.exe powrprof.dll,SetSuspendState Hibernate')
See win32api.ExitWindowsEx()
ActiveState documentation.
for flags: http://msdn.microsoft.com/en-us/library/aa376868%28v=vs.85%29.aspx
for hybernate/sleep:
http://msdn.microsoft.com/en-us/library/aa373201%28v=vs.85%29.aspx
to use this one you need to usectypes
since looks like pywin32
does not implement it.
Wakeup? I doubt you can execute code while sleeping. :)
Unfortunately, my reputation doesn't allow me to comment on an answer (yet). But I came here looking for an alternative because I was trying to avoid using the command line, so I just have to say this: I don't think calling SetSuspendState directly is the answer.
Take a look here for a reason: http://blogs.msdn.com/b/oldnewthing/archive/2004/01/15/58973.aspx (short version: bad things may happen. Long version: unexpected function signature corrupts the stack).
If you need another reason, it seems to simply... not work in Win7 (no matter what you pass as parameters, it always goes to hibernate - never to standby). At least that's what happened to me, and from what I read online I'm not the only one.
You can use the shutdown
command for that.
Example:
import os
os.system("shutdown -s -t 0")
This will shutdown the computer (-s) with a delay of 0 seconds (-t 0) for an instant shutdown. For the full usage, go here: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown
精彩评论