geocheats2
geocheats2

Reputation: 31

Add windows commands in python

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

Upvotes: 3

Views: 5058

Answers (1)

Aaron Maenpaa
Aaron Maenpaa

Reputation: 122890

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)

Upvotes: 8

Related Questions