osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to execute shell command in Python program?


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>