The example in this article describes how Python implements the process of piping. Share to everyone for your reference. The specific analysis is as follows:
What is the most dazzling magic that can be performed under Linux? believe that different people say different, but if there is no pipeline, then I am afraid that the magic will lose the magic
Here's how to use Python to handle these pipes.
Pipe Call subroutine
We want to use a subroutine in the program, but need dynamic transfer parameters (here is the dynamic, refers to according to the second-son program input results to determine what this input), how to do, do not panic, there are subprocess!
Below I will first introduce an example code, as well as his output results!
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
#!/usr/bin/python from subprocess Import * subprocess management, can do a lot of subprocess files, we want to introduce this file P = Popen (["Cat", "n"], bufsize= 1024,stdin=pipe, Stdout=pipe, close_fds=true) # Open Program The first argument is a list (program name, parameter) # The second parameter is the buffer size # Stdin,stdout is setting whether to open these pipes, If his value is subprocess. Pipe the words, # will open, same as stdin and stderr # Close_fds set to True (unix-only) all file descriptors are closed (FIN, 0,1,2) except before subroutine execution (Fout, P.stdout) for I in Range (10): # You'll understand. ^_^ Fin.write ("line" + str (i)) fin.write (' n ') Fin.flush () print fout.readline () |
The result of his output is:
?
1 2 3 4 5 6 7 8 9 10 |
Line0 line1 line2 line3 line4 line5 line6 line7 line8 line9 |
I hope this article will help you with your Python programming.