3

Can anyone tell me how to add the shutdown.exe to python and how . i also want to set and variables like shutdown.exe -f -s -t 60

1 Answer 1

8

The subprocess module allows you to run external programs from inside python. In particular subprocess.call is a really convenient way to run programs where you don't care about anything other than the return code:

import subprocess
subprocess.call(["shutdown.exe", "-f", "-s", "-t", "60"])

Update:

You can pass anything you want as part of the list so you could create a shutdown() function like this:

import subprocess

def shutdown(how_long):
    subprocess.call(["shutdown.exe", "-f", "-s", "-t", how_long])

So if we wanted to get user input directly from the console, we could do this:

dt = raw_input("shutdown> ")
dt = int(dt) #make sure dt is actually a number
dt = str(dt) #back into a string 'cause that's what subprocess.call expects
shutdown(dt)
Sign up to request clarification or add additional context in comments.

2 Comments

how can i add a user input instead of the number 60??
you just use a variable? ensure that it's a number casting it to int, or you'll have very unpleasant consequences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.