在 shutdown 命令中使用变量

Using a variable inside the shutdown command

我的脚本的目标是从用户那里获取他们希望在关机前多长时间以及他们希望在关机过程中显示的消息的输入。我的问题是我无法弄清楚如何将变量放入关闭命令并使其正确执行。

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)

您需要 assemble(通过连接)字符串 shutdown 以便它与您想要的完全匹配,包括注释周围的引号。

为此,最好对连接中使用的字符串文字使用单引号,以便可以在字符串内自由使用未转义的双引号。

类似于:

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

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

print(shutdown)

一个典型的运行:

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