2

I'm using subprocess to call command line. For example,

subprocess.check_output(["echo", "Hello World!"])

This is for one argument. I want to call an executable called "myFuntion" and pass several arguments to it, by their names. The command line I would have written is:

./myFunction --arg1 a1 --arg2 a2 

How do I "translate" this?

1 Answer 1

4

Simple: each argument is a separate element in the list.

So, if you understand how the shell would separate out this command line:

./myFunction --arg1 a1 --arg2 a2

… you know what the arguments are. When there are no quotes or escapes or anything else fancy, the shell just separates on spaces. So this is just 5 arguments:

subprocess.check_output(["./myFunction", "--arg1", "a1", "--arg2", "a2"])

If you have a command line that you're not sure how to translate, you can ask your shell to do it for you… or just use the shlex module in Python:

>>> print(shlex.split('./myFunction --arg1 a1 --arg2 a2'))
['./myFunction', '--arg1', 'a1', '--arg2', 'a2']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.