Add windows commands in python

Can someone tell me how to add shutdown.exe in python and how. I also want to set variables like shutdown.exe -f -s -t 60

+3
source share
1 answer

The subprocess module allows you to run external programs from within python. In particular, subprocess.call is a really convenient way to run programs where you don’t care about anything except the return code:

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

Update:

You can pass whatever you want as part of a list to create a function shutdown()as follows:

import subprocess

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

So, if we want to get user input directly from the console, we can 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 what subprocess.call expects
shutdown(dt)
+8
source

Source: https://habr.com/ru/post/1743986/


All Articles