- 1 year ago
How to put computer to sleep using subprocess.call?
To put a computer to sleep using the subprocess.call
function in Python, you can utilize the rundll32.exe
Windows command to call the PowrProf.dll
library and execute the sleep function. Here's how you can achieve it:
pythonimport subprocess
def put_computer_to_sleep():
try:
subprocess.call(["rundll32.exe", "powrprof.dll,SetSuspendState", "0", "1", "0"])
except Exception as e:
print("Error:", e)
# Call the function to put the computer to sleep
put_computer_to_sleep()
In the code above, the rundll32.exe
command is used to call the SetSuspendState
function from the PowrProf.dll
library with the necessary parameters to put the computer into sleep mode. The parameters "0"
, "1"
, and "0"
correspond to the sleep, force, and wakeup values, respectively.
Keep in mind that this method will put the computer into a low-power sleep mode. The effectiveness of this code may depend on the system's power management settings and hardware capabilities. Also, make sure to run the script with administrative privileges, as the SetSuspendState
function requires administrative rights to work properly.