Python Language Calling External Commands


Example

The simplest use case is using the subprocess.call function. It accepts a list as the first argument. The first item in the list should be the external application you want to call. The other items in the list are arguments that will be passed to that application.

subprocess.call([r'C:\path\to\app.exe', 'arg1', '--flag', 'arg'])

For shell commands, set shell=True and provide the command as a string instead of a list.

subprocess.call('echo "Hello, world"', shell=True)

Note that the two command above return only the exit status of the subprocess. Moreover, pay attention when using shell=True since it provides security issues (see here).

If you want to be able to get the standard output of the subprocess, then substitute the subprocess.call with subprocess.check_output. For more advanced use, refer to this.