python - 在 shutdown 命令中使用变量

标签 python variables input shutdown

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

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)

最佳答案

您需要组装(通过连接)字符串 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"

关于python - 在 shutdown 命令中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901432/

相关文章:

java - 如何检查鼠标是否被压出 JFrame

python - 如何在 mxnet 中进行加权 softmax 输出自定义操作?

python - 替换数据框中列表的名称

bash - Bash 命令中单引号内变量的扩展

jquery - 使用选择选项填充输入=文本

java - 如何在java中从键盘输入一些单词

python - 如何在 OpenCV 的矩形中使用归一化坐标

python - 将路径添加到 sys.path 与 PEP E402

java - 为什么对象的类型是child时,使用parent变量的值而不是child变量的值?

variables - 在 swift 中使用哪个声明