How to execute shell command in Python program?

Peter J. Holzer hjp-python at hjp.at
Sat Jul 20 13:07:22 EDT 2019


On 2019-07-20 15:39:58 +0100, Chris Narkiewicz via Python-list wrote:
> Madhavan Bomidi wrote:
> > import subprocess
> > subprocess.call(['./opac'],shell=True)

There may be an os.chdir() missing here.

> subprocess.call(['./opac', "my-input.inp"], shell=True)

We don't know whether the OP's program accepts command line arguments.
But I agree that it probably does and then this is the best way.


> The array takes command with a list of arguments. This
> way you don't need to do space escaping and other
> Jujitsu gimmicks.

In this case you should set shell=False. Either invoke the command
directly with an argument vector (almost always preferrable), or invoke
the shell with a shell command. Don't mix them.

        hp


-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190720/4a3ee528/attachment.sig>


More information about the Python-list mailing list