I'd like to write a short python script that puts my computer to sleep. I'Ve already searched the API but the only result on suspend has to do with delayed execution. What function does the trick ?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Without resorting to shell execution, if you have pywin32 and ctypes:
If you want just a one-liner with pywin32 and has the right permissions already (for a simple, personal script):
Note: if your system has disabled hibernation, it will suspend. In the first function I included a check to at least warn of this.
You can run shell command from python script. See subprocess module and then search for appropriate command for your operating system.
If you're using Windows, see: http://permalink.gmane.org/gmane.comp.python.windows/7382
Get pywin32, it also contains win32security if I remember correctly. Then try the mentioned script again.
I don't know how to sleep. But I know how to Hibernate (on Windows). Perhaps that is enough?
shutdown.exe
is your friend! Run it from the command prompt.To see its options do
shutdown.exe /?
I believe a hibernate call would be:
shutdown.exe /h
So, putting it all together in python:
But as other have mentioned, it is bad to use os.system. Use the popen instead. But, if you're lazy like me and its a little script them meh! os.system it is for me.