The official python documentation provides a detailed explanation of the subprocess in python and how to use the python subprocess with pipes, and it is beneficial for understanding and development purposes. However, the complete documentation can be a bit overwhelming for beginners. Moreover, the syntax might vary from one version to another version of python, which may confuse the beginners.
Hence, we have a solution for you, and here we explain the step-by-step process of using the python subprocess with pipes. We will explain the process with the help of examples, so let us go further to see the examples but, first, let us see what a pipe is to a subprocess in python. The PIPE in python is used to send or receive data from a program that is running as a subprocess in python. To do this, we can use a combination of pipe which is basically creating the pipe, a fork is used to create the subprocess in the program, dup2 is used to force the subprocess to use the pipe as a standard input and output channel, and lastly, exec is used to execute or run the new program. Moreover, Popen and Pclose are used to open or close the program, respectively.
The benefit of using Popen and Pclose is that it suggests a simple and easy-to-use interface. However, it does not provide much flexibility because it directly uses low-level functions. Let us see the examples below.
Example 1:
The first example will explain how to encode the command that contains a pipe and a redirection. Let us see the example:
Here is the python code that explains how to implement the Unix command with subprocess in python.
stdout=subprocess.PIPE)
fout = open('out.gz', 'wb')
p2 = subprocess.run(['pigz'], stdin=p1.stdout, stdout=fout)a
Note that the Popen() method is used with the first subprocess, while the run() method is used with the second call that encodes the redirections. The subsequent error will be caused if the run() method is used with the pipe itself.
Example 2:
This example will explain how a python program asks a user’s name. Then echo’s it with a greeting message and repeatedly asks for the names until the user enters the ‘exit.’ When the user enters ‘exit’ in the reply of asking the name, the if condition met with the given criteria and execution of the program stops. See the code given below.
print ("what is your name?")
for name in iter(sys.stdin.readline, ''):
name = name[:-1]
if name == "exit":
break
print ("Well, how are you {0}?".format(name))
print ("\n What is your name?")
Here is the repeated output of the above code that only stops when the user enters the word ‘exit.’
Example 3:
This example is a little complicated but easy to understand and use. It will explain how to control the input and output of a program using PIPE and subprocess in python. Let us see the code first.
import sys
proc = subprocess.Popen(["python", "CallMyName.py "])
while proc.returncode is None:
proc.poll()
proc = subprocess.Popen(["python", "CallMyName.py "],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdin.write("Alex\n")
proc.stdin.write("Jhon\n")
proc.stdin.close()
while proc.returncode is None:
proc.poll()
print("I am back from the child program this:\n{0}".format(proc.stdout.read()))
The subprocess.Popen() takes two named arguments, one is stdin and the second is stdout. Both of these arguments are optional. These arguments are used to set the PIPE, which the child process uses as its stdin and stdout. The subprocess.PIPE is passed as a constant so that either of the subprocess.Popen() or subprocess.PIPE the user specifies that they want the resultant. A child process named CallMyName.py is created in the program. Two names are passed to the CallMyName.py before sending the EOF signal to the input of the child. The mother process waits for the child process to finish before it reads the output produced by the child process. The following is the output of the above-given code.
As you can see, it is straightforward to use PIPE with the subprocess in python. If you follow the examples given above, you can easily learn the usage of PIPE with subprocesses in python.
Conclusion:
This article is about how to use the python subprocess with PIPE. First, we briefly introduced what a PIPE is to a subprocess in python, and then we provided some examples to explain how to use the PIPE with a subprocess in python.