它没有在subprocess.call(python)中正确拾取变量

发布于 2025-01-25 03:59:13 字数 760 浏览 0 评论 0原文

我正在创建一个电报机器人,以将我要求的信息发送到应用程序。

当我运行下面的代码时,它可以正常工作,除了最后一部分,当它执行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕色琉璃 2025-02-01 03:59:13

正确的版本:

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', f'--server {f}'])

格式的字符串文字或f-string是一个字符串字面,前缀为“ f”或“ f”。这些字符串可能包含替换字段,这是由卷曲括号{}界定的表达式。尽管其他字符串文字始终具有恒定值,但格式化的字符串是在运行时实际评估的表达式。

另请参见 pep 498

the right version:

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', f'--server {f}'])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文