How to call external commands in Python?

Invoking internal or external commands of the operating system as well as starting any other application available in the system can be achieved with the help of certain functions defined in two built-in modules - os module and subprocess module.

First, we shall use functions in the os module.

os.system() Function

This function receives a string argument, which has to be the valid executable command. This function opens a subshell by calling the system() function, a standard C function. Output if any of the commands will not appear on the Python console. The function returns 0 on the successful execution of the given command, otherwise, it returns 1.

import os
os.system("dir *.md")

The above code snippet will display .md files in the current folder. It is also possible to call any application whose executable (.exe) is found in the system path. The following will start calculator application in a Windows OS in a separate window.

import os

os.popen() function

This function opens a pipe stream for communication with a file like object created by the first argument command. This function has the following signature:

os.popen(cmd, mode, buffering)
  1. cmd: A string representing command to be called
  2. mode: Either 'r' or 'w' decides direction of pipe.
  3. buffering: system's buffering policy

The following command opens an input pipe to read line by line output of given dir command. Output is displayed on console of Python shell.

Example: Executing the DOS Command
import os
pipe=os.popen("dir *.md")
print (
 Volume in drive C is Windows 10
 Volume Serial Number is 540D-CE99

 Directory of C:\Users\User\Documents\MyDocs

10/04/2020  12:48 PM             2,182
10/07/2020  09:34 PM             4,639
10/08/2020  12:54 AM             3,696
10/06/2020  12:57 AM             3,315
               4 File(s)         13,832 bytes
               0 Dir(s)  175,492,399,104 bytes free

The subprocess module of Python's standard library has two functions meant for calling external commands. The purpose of functions in this module is to spawn a new process and connect to IO pipes.

As per PEP 324, it is recommended to use the run() function to invoke a new process. function

This function runs the command in the string argument, waits for the process to complete, and object of CompletedProcess class. Following use case of run() function displays .ipynb files in the current directory on the Python shell and returns CompletedProcess instance with returncode=0 indicating successful execution.

import subprocess"dir *.ipynb", shell=True)
CompletedProcess(args='dir *.ipynb', returncode=0) function

The function is a part of older version of this module (before Python 3.5). This is similar to the run() function, but returns returncode attribute.

import subprocess"cls", shell=True)

Above command calls the DOS command cls. The current shell terminal is cleared and the function returns 0 as returncode.