Reputation: 1125
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?
Upvotes: 3
Views: 3255
Reputation: 4382
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.
Upvotes: 13