how to run a C code with argc argv from python?

549 views Asked by At

I need to run my c code from python, usually as adviced here I do this and works perfectly:

from subprocess import call
call(["./code", "args", "to", "code"])

I would like to run the code that in order to run needs argv, as a number, so for instance normally from shell I should call simply:

./code #certainNumber

I would like to pass a string at the call function, like:

D=1
str = "./code %d"(%D)
call([str, "args", "to", "code"])

obviously this does not work. I would like to chose from python the parameter that I need to insert in my c code.

thanks

2

There are 2 answers

0
user3585292 On BEST ANSWER

As twalberg said this works perfectly:

call(["./code", str(variableContainingNumber), "other", "args"])
0
AndrewGrant On

You could always do the following

import os

# arguments is some list of arguments as strings
output = os.system(processname + " ".join(arguments))