我的脚本的目标是从用户那里获取他们在关闭前需要多少时间的输入,以及他们想要在关闭过程中显示的消息。我的问题是我无法确切地知道如何将变量放入关闭命令并使其正确执行。
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"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句