Skip to content Skip to sidebar Skip to footer

Python Subprocess Passes One Argument Only

I have a Python script (2.7) which I use to invoke an external process.Till recently it worked fine. But now when I run it I see it doesn't pass over process arguments.I have also

Solution 1:

When using shell=True, just pass a string (not a list);

p = subprocess.Popen('./myapp -p s', shell=True)
p.communicate()

Update

Always prefer;

  • shell=False (the default) to shell=True and pass an array of strings; and
  • an absolute path to the executable, not a relative path.

I.e.;

with subprocess.Popen(['/path/to/binary', '-p', 's']) as proc:
    stdout, stderr = proc.communicate()

If you're just interested in the stdout (and not the stderr), prefer this to the above solution (it's safer and shorter):

stdout = subprocess.check_output(['/path/to/binary', '-p', 's'])

Solution 2:

Don't use shell=True:

p = subprocess.Popen(["./myapp","-p","s"])
p.communicate()

Post a Comment for "Python Subprocess Passes One Argument Only"