How to Get Return Code from Process in Python Subprocess Execution?

How to Get Return Code from Process in Python Subprocess Execution?

A process is a name for a running program. Memory, lists of files, and a program counter that takes account of the instructions being implemented, and a call stack that retains the local variables are all part of each process’s system state. A process normally processes statements one after the other in a single command flow sequence known as the process’s main thread. The program only does one thing at any given moment. Our computer is always running subprocesses. Every action we take on our computer entails activating a subprocess. Even if we are constructing a basic “hello world” program in Python. Even if you have been programming for a while, you might not know the concept of a subprocess. The principles of the subprocess will be covered in this article, as well as how to use the Python subprocess standard library.

The commonly used modules are os.fork(), subprocess.Popen(), and others. Subprocesses, on the other hand, run as totally separate entities, each with its own unique system state and the main thread of operation. You can use the Subprocess module of Python to construct new processes. After that, it can connect to normal input/output/error streams and get a return code. For example, a subprocess can run any Linux command from a script. Depending on the situation, either retrieve the result or simply verify that the operation was executed appropriately. Please note that the syntax of the subprocess module has changed in Python 3.5.

Example 1:

In the first example, you can understand how to get a return code from a process. The code shows that we have imported the subprocess module first. After that, we have called the Popen method. The return value is essentially a pipe-attached open file object. Depending on the w and r mode’ it can be written or read. “r” is the default mode. Some programs use the bufsize arguments that are nearly identical to the built-in open() function’s analogous argument. The difference is that when the exit status is zero, in which case None is returned, the command’s exit status is available as the return result of the file object’s close() method. We wanted to receive the output of the command, so we used the stdout parameter with the value subprocess. PIPE is used in the second line of code after importing the subprocess, as you can see.

import subprocess
p = subprocess.Popen([“echo”, “Welcome”], stdout=subprocess.PIPE)
print (p.returncode)

The above piece of code gives the below output.

Example 2:

After using communicate, we will teach you how to acquire the return code of a subprocess. The communicate() method essentially writes input, reads all output, and waits for the subprocess to quit (there is no input in this example, so it just closes the subprocess’ stdin to signify that there is no more input).

import subprocess
p = subprocess.Popen([“echo”, “Welcome”], stdout=subprocess.PIPE)
print (p.communicate()[0])

This is what the above code produces as output.

Example 3:

In this example program, we are going to use save process output. Use the save process output function to save the output of a code in a string. This method’s syntax is as follows:

The subprocess’s stdout method in Python takes a few parameters: args: It’s the command you are looking to run. Multiple commands can be passed by using a semicolon (;) to separate them. The value given as (os.pipe()) to the standard input stream is referred to as stdin. The obtained value of the standard output stream is stdout. Any errors from either the standard error stream are handled by stderr. If true, the shell boolean parameter causes the process to run in a new shell. The universal newline is a boolean parameter and when its value is true, it opens files with stdout and stderr.
import subprocess.

p = subprocess.check_output([“echo”, “Phython is easy to learn!”])
print(p)

The result can be seen here.

Conclusion:

You learned about subprocesses in Python and how to extract the return code from a process in this post. By incorporating the subprocess module, you can easily run external processes directly from your Python code. The Popen, communicate, and returncode methods are the most important functions in this article. Python technique popen() establishes a connection to or from a command.

The bufsize argument is equivalent to the open() function’s size argument. The communicate() method effectively writes input, collects all output, and awaits for the subprocess to exit. The returncode() function gives the return code specified by the performed command. The command runs in its own process and does not need any input from the user. We have explained these methods in our provided examples. You won’t have much issue generating and communicating with subprocesses in Python once you practice and learn how to utilize these functions correctly.

Leave a Reply

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