3

I am using Python's subprocess module to launch another program. The program requires an argument '-c{0-7}'.

this_dir = os.path.dirname(os.path.abspath(__file__))
cmd = [os.path.join(this_dir,'foobar'),'-c%d' % channel]
print "Starting process: %s" % str(cmd)
Proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)

In the C++ program, I'm checking the arguments passed in:

for (int i = 0; i < argc; i++)
{   
    cerr << i << "   " << argv[i] << endl;
}   
cerr << "" << endl;

Here is the output when I run the python script:

user@home:~/embedded_pqa/saleae$ ./foobar.py -c3
Starting process: ['/home/user/code/foobar', '-c3']
0   /home/user/code/foobar

As is clear, the argument '-c3' is not being passed to the subprocess. Any thoughts?

1
  • A tool like strace or truss is the right thing to see what actually is or isn't being passed to the execve syscall. Also, shell=True is evil; if you want control of how your arguments are passed, don't use it. Commented May 7, 2012 at 23:34

1 Answer 1

13

The issue is with shell=True. Quoting the docs:

On Unix, with shell=True: […] If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.

That means it calls the following command:

sh -c /home/user/code/foobar -c3

which the shell interprets as the command /home/user/code/foobar and an additional shell parameter -c3.

Just get rid of shell=True since you aren't using any sh features anyway and you're using an already separated argument list yourself.

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

1 Comment

Thanks for the help. shell=True was the culprit.

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.