As a Python developer, it’s possible you’ll need to get a course of’s return code for a number of causes. As an illustration, whereas dealing with errors, you will need to know whether or not a subprocess was efficiently executed or triggered any error. Furthermore, the returned code can be utilized for monitoring this system or subprocess execution.
This weblog will cowl:
Methodology 1: Getting Return Code From Course of in Python Utilizing “returncode” Attribute
In Python, the “returncode” attribute belongs to the “subprocess” module. This attribute comprises an integer worth that refers back to the exit standing of a subprocess upon execution. Extra particularly, the worth “0” signifies the profitable execution of the subprocess, whereas a non-zero worth signifies an irregular situation or an error.
Syntax
Right here, “course of” represents the method object related operate of the “subprocess” module, and the “returncode” is its corresponding return code worth.
Instance
To make the most of the “returncode” attribute for fetching the return code in Python subprocess execution, try the supplied code:
course of = run([‘echo’, ‘linuxhint user!’])
print(‘return code:’, course of.returncode)
In keeping with the given code:
- Initially, import the “run()” operate of the “subprocess” module.
- The “run()” operate runs the “echo” command and waits for it to finish, finally returning the method object.
- Lastly, the “print()” operate shows the “returncode” attribute worth of the retrieved object:
Right here, “0” has been returned, which signifies that the desired subprocess has been executed efficiently.
Methodology 2: Getting Return Code From Course of in Python Utilizing “talk()” Methodology
The “talk()” methodology in Python permits the interplay with a course of by passing enter to it. This methodology outputs a tuple that includes “std_out” (customary output) on the 0th index and “stderr_data” (error stream or messages) on the 1st index. The values of those related variables can be returned as “None” if the usual output or the error messages haven’t been captured.
Syntax
course of.talk(enter=None, timeout=None)[n]
Within the given syntax, “enter” is a byte string handed to the method’s customary enter, “timeout” is the variety of seconds to attend for the method completion, and “n” refers to index, the place “0” outputs the usual output, and “1” signifies the error streams.
Instance
Now, execute the next Python code for getting the return code utilizing the talk() methodology:
course of = subprocess.Popen([“echo”, “linuxhint user!”], stdout=subprocess.PIPE)
print (course of.talk()[0])
Right here:
- Firstly, import the “subprocess” module.
- Its “Popen()” methodology generates a Popen object that represents the newly created subprocess and returns it.
- The primary argument of the Popen() refers back to the command that must be executed, and the worth of the “stdout” is about because the “subprocess.PIPE” for capturing the subprocess output.
- Lastly, the “course of.talk()[0]” returns the usual output of the subprocess:
In our case, the “b’linuxhint consumer!n’” has been displayed because the returned code.
Methodology 3: Getting Return Code From Course of in Python Utilizing “check_output()” Methodology
The “check_output()” Python methodology runs a command as a subprocess and saves its output. This methodology presents the output as a byte’s object.
Syntax
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None, encoding=None, errors=None)
Right here, “args” signifies the instructions that have to be executed, whereas all the different parameters are elective.
Instance
We have now handed the “echo” command and the respective string to the check_output() as an argument. Resultantly, this methodology will return the captured output:
course of = subprocess.check_output([“echo”, “Hello from linuxhint!”])
print(course of)
Output
Bonus Tip: Decode the Commonplace Output
Python additionally provides the power to decode the returned customary output with the “decode()” methodology. On this code, we are going to decode the byte string using the “utf-8” encoding scheme:
print(course of.decode(“utf-8”))
Output
That was all about getting the return code in Python.
Conclusion
To get the return code from a course of in Python subprocess execution, use the “returncode” attribute, “talk()”, or the “check_output()” methodology. The “returncode” attribute returns “0” in case of profitable execution, and the “talk()[0]” outputs the usual output saved on the zero index. Furthermore, the “check_output()” methodology additionally works the identical. This weblog lined a number of strategies for getting return code in Python.