Call External Program in Python

  1. Use the os.system() Function to Execute External System Commands in Python
  2. Use the os.popen() Function to Execute External System Commands in Python
  3. Use the subprocess.Popen() Function to Execute External System Commands in Python
  4. Use the subprocess.call() Function to Execute External System Commands in Python
  5. Use the subprocess.run() Function to Execute External System Commands in Python

In Python, we have different modules and functions available to communicate and get responses from the OS. Python also has methods that can execute external programs.

In this tutorial, we will discuss how to call an external program in Python.

Use the os.system() Function to Execute External System Commands in Python

The os module in Python can interact with the device’s OS and has functions available to execute shell commands. The system() function implements a command, passed to it as a string, in a subshell. It returns the exit status of the shell, which the OS returns after running the given command. It allows to run multiple commands at once and call external programs, not shell commands.

For example,

import os
os.system("echo Hello World")

Use the os.popen() Function to Execute External System Commands in Python

Another function from the os module to run external program commands is the popen() function. This method opens a pipe related to the command. It is similar to the os.system() function, but it provides a file-type object that helps with input-output operations related to the command. We can specify the file-mode for the file and the buff-size as the parameter in the function.

For example,

import os
os.popen("echo Hello World")

Note that there are 4 types of popen() function available in Python 2. The difference between them is with the output of these 4 functions.

Use the subprocess.Popen() Function to Execute External System Commands in Python

The subprocess module is similar to the os module and has improved functions and methods for calling external programs. The Popen is a class and not a method. It is a little complicated to use this due to various parameters that need to be specified along with the required command. It combines all the 4 os.popen() functions into one. It also executes the external program as a separate process. The following code shows how to use this.

import subprocess
subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read()

Use the subprocess.call() Function to Execute External System Commands in Python

The call() function is an improvement to the popen() function. However, unlike the popen() function, It waits for the command to execute before providing the return code. We pass the command as an argument to the function.

For example,

import subprocess
subprocess.call("echo Hello World", shell=True)  

Use the subprocess.run() Function to Execute External System Commands in Python

In Python v3.5 and above, the run() function is more often used for running program commands. It returns a completed-process object after running the command, passed to the function as a parameter.

For example,

import subprocess
print(subprocess.run("echo Hello World", shell=True))  

Note that the subprocess module is preferred over the os module for running external commands in Python.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Process

  • Background Process in Python
  • Perform a Parallel Processing in Python