wintermute
wintermute

Reputation: 21

How to put computer to sleep using subprocess.call?

I'm in the middle of making a Python script to shutdown, restart, hibernate or put to sleep after a few seconds.

I know its subprocess.call(["shutdown", "/s"]) to shutdown, "/r" to restart and "/h" to hibernate.

How can you put the computer to sleep using call() on Windows 10?

Upvotes: 2

Views: 1302

Answers (2)

Minaga Rajapakshe
Minaga Rajapakshe

Reputation: 11

I know this is asked 3 years ago, but if anyone looking for help, I found a way for sleep with pyautogui

First download and install pyautogui with terminal using command pip download pyautogui and pip install pyautogui

  1. Import it

import pyautogui

  1. use this code (change if you need)
pyautogui.hotkey("alt", "f4")
time.sleep(1)
pyautogui.press("left")
time.sleep(1)
pyautogui.press("enter")

Visit this website if you need any help with pyautogui commands

(I am too new to python coding, there must be easy and more suitable ways to do this. Sorry if I sound so noob.)

Upvotes: 1

dmuensterer
dmuensterer

Reputation: 1885

Shutdown doesn't have a function for Sleep integrated however you can use the following: rundll32.exe powrprof.dll,SetSuspendState 0,1,0

Upvotes: 2

Related Questions