它没有在subprocess.call(python)中正确拾取变量
我正在创建一个电报机器人,以将我要求的信息发送到应用程序。
当我运行下面的代码时,它可以正常工作,除了最后一部分,当它执行subprocess.call(['robocop',' - robobocop',' - cpu',' - server {f} '])
它似乎没有放置变量“ f”的正确数据,因为它为我提供了python代码所在的服务器的信息,但不是我请求的服务器。
Robocop脚本是我在Bash中创建的应用程序,
# Check CPU Status
@bot.message_handler(commands=['cpu'])
def command_long_text(m):
cid = m.chat.id
f = (m.text[len("/cpu"):])
bot.send_message(cid, "Collecting CPU information from " + f)
bot.send_chat_action(cid, 'typing')
time.sleep(3)
subprocess.call(['robocop', '--robocop', '--cpu', '--server {f}'])
您能帮我吗?
如果我将变量更改为字符串,则可以正常工作。
subprocess.call(['robocop', '--robocop', '--cpu', '--server', 'appdb'])
谢谢你!
I am creating a telegram bot to send the information that I request to the app.
When I run the code below it works fine except for the last part, when it does the subprocess.call(['robocop', '--robocop', '--cpu', '--server {f}'])
it doesn't seem to put the correct data of the variable "f" because it gives me information of the server where the python code is, but not of the server that I request.
The robocop script is an app that I have created in bash
# Check CPU Status
@bot.message_handler(commands=['cpu'])
def command_long_text(m):
cid = m.chat.id
f = (m.text[len("/cpu"):])
bot.send_message(cid, "Collecting CPU information from " + f)
bot.send_chat_action(cid, 'typing')
time.sleep(3)
subprocess.call(['robocop', '--robocop', '--cpu', '--server {f}'])
Could you please help me?
If I change the variable to a string it works correctly.
subprocess.call(['robocop', '--robocop', '--cpu', '--server', 'appdb'])
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的版本:
格式的字符串文字或
f-string
是一个字符串字面,前缀为“ f”或“ f”。这些字符串可能包含替换字段,这是由卷曲括号{}界定的表达式。尽管其他字符串文字始终具有恒定值,但格式化的字符串是在运行时实际评估的表达式。另请参见 pep 498 。
the right version:
A formatted string literal or
f-string
is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.See also PEP 498.