简体   繁体   中英

How do I put my laptop into sleepmode after a given time?

So I'm writing this code to put my laptop in sleep mode after a given amount of time. Weird thing is, when the program wants to execute the last line of code it points out that there's a syntax error at os .

I've tried putting a simple instead print command (as a test) that also ends up as a syntax error. I've tried the command inside os.system() directly and that works.

The python version I use is 3.7.2.

import time
import sys
import os
counter = 0
for x in sys.argv:
    counter+= 1

if counter== 2:
    seconds = sys.argv[1]

else:
    seconds =  sys.argv[2]+60*sys.argv[1]
time.sleep(int(seconds)
os.system('Rundll32.exe Powrprof.dll,SetSuspendState Sleep')

I expect the script to put my PC into sleep mode after x seconds or after x minutes, y seconds (dependent on parameters).

This is how I enter the command in CMD:

python file.py x (y)

Yes I'm in the correct folder to do so.

As @SLaks pointed out:

You're missing a )

However, I see something very odd in your code, and that's this part of the code:

counter = 0
for x in sys.argv:
    counter+= 1

if counter== 2:
    seconds = sys.argv[1]

else:
    seconds =  sys.argv[2]+60*sys.argv[1]

You don't need to loop through sys.argv to find out its length , you can use len() !

Like this:

seconds = int(sys.argv[1]) if(len(sys.argv) == 2) else (int(sys.argv[2])+60*int(sys.argv[1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM