Search code examples
windowspython-3.xshutdown

Trying to make simple timer to shutdown windows in python


Trying to make simple timer to shutdown windows in python (just for fun) but i'm having a small problem that can't find the answer, the script just ask if the user wants to use minutes or seconds to shut down using an if, the part of using seconds works fine, the problem is with minutes, the script get the time in minutes and convert to seconds, than runs: subprocess.call(["shutdown" "-s", "-t", ntime])

But it's not working, if i double click on the file.py and try this part, the script just close, but if a execute in the IDLE i recieve an this error:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\shutdown.py", line 17, in <module>
    subprocess.call(["shutdown" "-s", "-t", ntime])
  File "F:\Programs\python\lib\subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "F:\Programs\python\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "F:\Programs\python\lib\subprocess.py", line 997, in  _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>


Code:
import subprocess
print('0:Seconds')
print('1:Minutes')
minorsec = input('Want to use minutes or seconds? ')
if minorsec == '0':
    time = input('Type the time to shutdown in seconds: ')
    subprocess.call(["shutdown", "-s", "-t", time])
elif minorsec == '1':
    time = input('Type the time to shutdown in minutes: ')
    ntime = int(time) * 60
    subprocess.call(["c:\\windows\\system32\\shutdown.exe" "-s", "-t", str(ntime)])
else:
    print('Error, Press just 0 or 1')
input('Press Enter to close: ') 

Solution

  • Issue #1: This line

    ntime = time * 60
    

    Doesn't do what you think it does. time, the value returned from the previous input call is a string, not an integer. So if the user types "15" as his input, ntime becomes something insane like: "1515151515151515151515.....15". That's probably your core problem:

    Convert the user's input from string back to integer:

     ntime = int(time) * 60
    

    Issue #2, which is a side effect of fixing #1: All the values in the argument list to subprocess.call must be strings:

    time = input('Type the time to shutdown in minutes: ')
    ntime = int(time) * 60
    subprocess.call(["shutdown" "-s", "-t", str(ntime)])
    

    Issue #3: Don't use an argument list:

    subprocess.call("shutdown.exe -s -t " + str(ntime))