More about the Python module subprocess and subprocess classes

We already have a brief description on Python module “subprocess.” It’s a commonly used Python module for SysAdmin jobs. This module should be used for accessing system commands. This is the simplest way for running Linux commands. Yeah, that’s what we really need!

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Read the following doc for more details:

The module subprocess in Python

Here I would like to integrate directly Python and Linux which will be helpful for Linux SysAdmins! Bookmark the Index page and read more about Python.

In this article, I am going to list out the commonly using subprocess classes in Python. The available classes in Python subprocess module are listed below:

subprocess.CalledProcessError  subprocess.check_output        subprocess.pickle
subprocess.MAXFD               subprocess.errno               subprocess.select
subprocess.PIPE                subprocess.fcntl               subprocess.signal
subprocess.Popen               subprocess.gc                  subprocess.sys
subprocess.STDOUT              subprocess.list2cmdline        subprocess.traceback
subprocess.call                subprocess.mswindows           subprocess.types
subprocess.check_call          subprocess.os

Here I am trying to explain commonly using classes with simple examples.

1. subprocess.call

We are familiar with the class “subprocess.call” as we discussed it in the fist post on module subprocess. Please see the aforementioned link for more details.

2. subprocess.check_call

This class will be helpful to identify more about the error. Basic usage or syntax is pasted below:

Syntax

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Once executed, it runs the command with arguments and wait for the command to complete. If the return code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute.

See the examples pasted below:

Remember, I am using “ipython” to illustrate it.

Access ipython and execute the following command:

import subprocess
subprocess.call(['df', 'h'])

See, it’s a wrong input, did you notice the error in syntax? There is no (-) with “h” argument. It will not give you the disk usage in human readable mode. See the difference between subprocess.call and subprocess.check_call.

In [1]: import subprocess
In [2]: subprocess.call(['df', 'h'])
df: ‘h’: No such file or directory
Out[2]: 1

Now execute the same with subprocess.check_call class in subprocess module

In [3]: subprocess.check_call(['df', 'h'])
df: ‘h’: No such file or directory
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
 in ()
----> 1 subprocess.check_call(['df', 'h'])

/usr/lib64/python2.7/subprocess.pyc in check_call(*popenargs, **kwargs)
    540         if cmd is None:
    541             cmd = popenargs[0]
--> 542         raise CalledProcessError(retcode, cmd)
    543     return 0

CalledProcessError: Command '['df', 'h']' returned non-zero exit status 1

It returns a detailed error with “CalledProcessError.”

3. subprocess.Popen and subprocess.PIPE classes

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *