1

I have one shell command that uses some argument that is being invoked from Python script :

cmd = 'sysbench --test=cpu --cpu-max-prime=20000 --num-threads=2 run > cpu_sysbench.txt'

Now I wanted to define -cpu-max-prime value as a varible and use it:

prime_numbers = "20000"
cmd = 'sysbench --test=cpu --cpu-max-prime=$prime_numbers --num-threads=2 run'

But when I run it I see few syntax errors:

3 Answers 3

5

Try:

cmd = f'sysbench --test=cpu --cpu-max-prime={prime_numbers} --num-threads=2 run'

This is called an f-string, it's an easy way to format strings in Python >= 3.6. Of course you could also just use string concatenation or other methods, this is not at all specific to running shell commands.

Sign up to request clarification or add additional context in comments.

3 Comments

this is what I see:cmd = f'sysbench --test=cpu --cpu-max-prime=20000 --num-threads=2 run > cpu_sysbench.txt' ^ SyntaxError: invalid syntax
The difference is that here we're using fstrings.
@FilipMłynarski The OP appears to be using something older than 3.5, so f-strings aren't available.
2

Use str.format

Ex:

prime_numbers = "20000"
cmd = 'sysbench --test=cpu --cpu-max-prime={} --num-threads=2 run'.format(prime_numbers)
# --> sysbench --test=cpu --cpu-max-prime=20000 --num-threads=2 run

5 Comments

Hello @Rakesh, actually the complete command is , "cmd = 'sysbench --test=cpu --cpu-max-prime=20000 --num-threads=2 run > cpu_sysbench.txt'"
Did you use cmd = 'sysbench --test=cpu --cpu-max-prime={} --num-threads=2 run > cpu_sysbench.txt'.format(prime_numbers) ?
Cool, this one worked for me, even for print it can be used ?
would like to use num-threads variable together with prime_numbers, what should be the exact syntax for same ?
cmd = 'sysbench --test=cpu --cpu-max-prime={} --num-threads={} run > cpu_sysbench.txt'.format(prime_numbers, num_threads) OR cmd = 'sysbench --test=cpu --cpu-max-prime={prime_numbers} --num-threads={num_threads} run > cpu_sysbench.txt'.format(prime_numbers=prime_numbers, num_threads=num_threads)
2

Don't use string formatting at all. Build a list consisting of the command name and its arguments as separate elements for use with subprocess.Popen (or one of its wrapper functions).

prime_numbers = "20000"
cmd = [
    'sysbench',
    '--test=cpu',
    '--cpu-max-prime',
    prime_numbers,
    '--num-threads=2',
    'run'
]

with open("cpu_sysbench.txt", "w") as f:
    subprocess.run(cmd, stdout=f)
    # or subprocess.call(cmd, stdout=f) in older Python 3.x

Update: to incorporate a grep command, use two subprocesses with their standard input and standard output joined.

with open("cpu_sysbench.txt", "w") as f:

    p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    # grep + cut can almost always be replaced by a single awk
    subprocess.call(['awk', '-F', ':', '/total time:/ { print $2 }'], stdin=p1.stdout, stdout=f)

5 Comments

tried running abobe snippets but seeing this, AttributeError: 'module' object has no attribute 'run'
What version of Python are you using? subprocess.run was introduced in 3.5.
actually if I run it using python3 script_name it works but if just run it ./script_name it fails
Also, have to apply grep command on the cpu_sysbench.txt file, cmd = [ 'grep', 'total time:', "cpu_sysbench.txt" | cut -d ':' -f2 " ] but it seems to failing for syntax
Because | isn't an argument. If you want a good answer, you need to be clear about what you are actually asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.