How to Run a Shell Command from Python and Get The Output?

In Python, often you may want to execute linux command and get the output of the command as string variable. There are multiple ways to execute shell command and get output using Python. A naive way to do that is to execeute the linux command, save the output in file and parse the file.

import os
cmd = 'wc -l my_text_file.txt > out_file.txt'
os.system(cmd)

Get output from shell command using subprocess

A better way to get the output from executing a linux command in Python is to use Python module “subprocess”. Here is an example of using “subprocess” to count the number of lines in a file using “wc -l” linux command.

Let us first import the subprocess module

# import subprocess library
>import subprocess

Launch the shell command that we want to execute using subprocess.Popen function. The arguments to this command is the shell command as a list and specify output and error.

>out = subprocess.Popen(['wc', '-l', 'my_text_file.txt'], 
           stdout=subprocess.PIPE, 
           stderr=subprocess.STDOUT)

The output from subprocess.Popen is subprocess.Popen object. And this object has number of methods associated with it and we will be using communicate() method to get the standard output and error in case as a tuple.

Here standard output contains the result from wc -l command and the stderr contains None as there are no errors.

>stdout,stderr = out.communicate()
>print(stdout)
    3503 my_text_file.txt
>print(stderr)
None

We can then parse the stdout to get the result from shell command in Python, the way we want . For example, if we just want the number of lines in the file, we split the stdout

>stdout.split()[0]
'3503'