BrewCrew15 BrewCrew15 - 6 days ago 5
Python Question

Using a variable inside the shutdown command

The goal of my script is to get input from a user for how much time they want before shutdown and also the message they want to appear during the shutdown process. My problem is I cannot figure out exactly how to put the variables into the shutdown command and have it execute properly.

import os

time = (input("How much time till shutdown?"))

message = input("What is your shutdown message?")

shutdown = "shutdown /f /r /t", time "c", message

os.system(shutdown)

Answer

You need to assemble (by concatenating) the string shutdown so that it matches exactly what you want, including the quote marks around the comments.

For this purpose it is good to use single quotes for the string literals used in the concatenation so that unescaped double quotes can be freely used inside the strings.

Something like:

time = input("How much time till shutdown? ")
message = input("What is your shutdown message? ")

shutdown = 'shutdown /f /r /t ' + time + ' /c "' + message +'"'

print(shutdown)

A typical run:

How much time till shutdown? 60
What is your shutdown message? Goodbye
shutdown /f /r /t 60 /c "Goodbye"