Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 305
geocheats2 Avatar asked Mar 20 '26 22:03

geocheats2


1 Answers

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)
like image 186
Aaron Maenpaa Avatar answered Mar 23 '26 10:03

Aaron Maenpaa