Home > Software design >  It doesn't pick up the variable correctly in subprocess.call (python)
It doesn't pick up the variable correctly in subprocess.call (python)

Time:05-03

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!

CodePudding user response:

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.

  • Related