Subprocess in Python
In this tutorial, we will understand what is a subprocess in python and will understand how to use it.
Subprocess
A subprocess is a very prevailing portion of the python library. It is a module present in python that is used to run any new application or program by writing python codes. Moreover, it is also helpful in acquiring the input or output or error pipes or even the exit codes of several commands.
Under the subprocess module, two functions are used to implement dissimilar programs using Python. Let us understand each of these functions one by one.
1.Let us first understand the first function
subprocess.check_call ( args, *, stdin=None, stdout=None, stderr=None, shell=False )
Now, let us understand the Parameters used in the above method:args = It denotes the argument to be used in the program. It consists of the command to be implemented as a parameter. Numerous commands can be passed as a string but they should be separated by a semi-colon “;”.
stdin = It denotes the value of the standard input stream to be passed as (os.pipe ()).
stdout = It denotes the value of the output obtained from the standard output stream.
stderr = It denotes the value of fault obtained (if any) from the standard error stream.
shell = It denotes the boolean parameter. If the value returned is True then commands get executed through a new shell environment.
Return Value:
The return code of the command is returned by the above function. If the return code is zero, then the function simply returns (command executed successfully), or else an error named CalledProcessError is being arisen.
2. Let us understand the second function.
subprocess.check_output ( args, *, stdin=None, stderr = None, shell = False, universal_newlines = False )
Now, let us understand the Parameters used in the above method:
args = It denotes the argument to be used in the program. It consists of the command to be implemented as a parameter. Numerous commands can be passed as a string but they should be separated by a semi-colon “;”.
stdin = It denotes to the value of standard input stream to be passed as pipe(os.pipe()).
stdout = It denotes the value of output obtained from the standard output stream.
stderr = It denotes the value of error obtained (if any) from the standard error stream.
shell = It denotes the boolean parameter. If True the commands get executed through a new shell environment.
universal_newlines = Boolean parameter. If the value is true then, files containing stdout and stderr are unlocked in universal newline mode.
Return Value:
The return code of the command is returned by the above function. The function just returns the output as a byte string (command accomplished) if the return code is zero otherwise an error named CalledProcessError is being raised.
Let us now see the example to understand the concept better.
Considering a C program
Example 1:
#include<stdio.h>
int main ()
{
printf(" Bonjour world from C ");
// It would result in an exception when returned with any other non zero
// value when called from python
return 0;
}
Considering a C++ program
Example 2:
#include <iostream>
using namespace std;
int main ()
{
int a1, a2;
cin >> a1 >> a2;
cout << " Bonjour world from C++. Values are:" << a1<< " " << a2;
return 0;
}
Considering a JAVA program
Example 3:
class Bonjour {
public static void main (String args[])
{
System.out.print ("Bonjour world from Java.");
}
}
# Python 3 program to illustrate the usage of the subprocess module
import subprocess
import os
def excuteC():
# The return code of the c program(return 0) is stored
# and the result is displayed
sp = subprocess.check_call("gcc BonjourWorld.c -o out1;./out1", shell = True)
print(", return code", sp)
def executeCpp():
# creating a pipe to a child process
data, temp = os.pipe()
# writing to STDIN as a byte object (converting the string
# to bytes with encoding utf8)
os.write (temp, bytes("25 70\n", "utf-8"));
os.close (temp)
# storing the result of the program as a byte string in sp
sp= subprocess.check_output ("g++ HelloWorld.cpp -o out2;./out2", stdin = data, shell = True)
# decoding to a usual string
print(sp.decode("utf-8"))
def executeJava():
# storing the result of
# the java program
sp= subprocess.check_output("javac BonjourWorld.java;java HelloWorld", shell = True)
print(sp.decode("utf-8"))
# Below is a Driver function
if __name__=="__main__":
excuteC()
executeCpp()
executeJava()
Output:
