Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess Popen not sending all arguments to shell

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?

like image 215
Rusty Avatar asked Sep 08 '26 12:09

Rusty


1 Answers

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.

like image 171
koniiiik Avatar answered Sep 11 '26 02:09

koniiiik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!