Overview
Consider such a problem, there is hello.py script, output "Hello, world!" ; There are testinput.py scripts, waiting for user input, and then printing data entered by the user. So, how to send hello.py output to testinput.py, finally testinput.py print received "Hello, world!." Let me explain how the shell interacts with each other step-by-step.
The hello.py code is as follows:
Copy Code code as follows:
#!/usr/bin/python
Print "Hello, world!"
The testinput.py code is as follows:
Copy Code code as follows:
#!/usr/bin/python
str = raw_input ()
Print ("Input string is:%s"% str)
1.os.system (cmd)
This is done only by executing the shell command, which returns a return code (0 indicates that the execution succeeded or failed)
Copy Code code as follows:
Retcode = Os.system ("Python hello.py")
Print ("Retcode is:%s"% retcode);
Output:
Copy Code code as follows:
Hello, world!.
Retcode is:0
2.os.popen (cmd)
Executes the command and returns the input stream or output stream of the execution command program. This command can only operate one-way flows, with the shell command one-way interaction, not two-way interaction.
Returns the program output stream, connecting to the output stream with the Fouput variable
Copy Code code as follows:
Fouput = Os.popen ("Python hello.py")
result = Fouput.readlines ()
Print (' Result ':%s '% result);
Output:
Copy Code code as follows:
Result is: [' Hello, world!\n ']
Returns the input stream, connecting to the output stream with the Finput variable
Copy Code code as follows:
Finput = Os.popen ("Python testinput.py", "W")
Finput.write ("How are you\n")
Output:
Copy Code code as follows:
Input string is:how are you
3. Using the Subprocess module
Subprocess.call ()
Like Os.system (), note that the "shell=true" here means executing the command with the shell, rather than using the default OS.EXECVP ().
Copy Code code as follows:
F = Call ("Python hello.py", shell=true)
Print F
Output:
Copy Code code as follows:
Subprocess. Popen ()
Using Popen can be a two-way flow of communication, you can send a program output stream to another program's input stream.
Popen () is the constructor of the Popen class, and communicate () returns tuples (Stdoutdata, stderrdata).
Copy Code code as follows:
P1 = Popen ("Python hello.py", stdin = None, stdout = PIPE, shell=true)
P2 = Popen ("Python testinput.py", stdin = p1.stdout, stdout = PIPE, shell=true)
Print p2.communicate () [0]
#other Way
#print P2.stdout.readlines ()
Output:
Copy Code code as follows:
Input string Is:hello, world!
The consolidated code is as follows:
Copy Code code as follows:
#!/usr/bin/python
Import OS
From subprocess import Popen, PIPE, call
Retcode = Os.system ("Python hello.py")
Print ("Retcode is:%s"% retcode);
Fouput = Os.popen ("Python hello.py")
result = Fouput.readlines ()
Print (' Result ':%s '% result);
Finput = Os.popen ("Python testinput.py", "W")
Finput.write ("How are you\n")
F = Call ("Python hello.py", shell=true)
Print F
P1 = Popen ("Python hello.py", stdin = None, stdout = PIPE, shell=true)
P2 = Popen ("Python testinput.py", stdin = p1.stdout, stdout = PIPE, shell=true)
Print p2.communicate () [0]
#other Way
#print P2.stdout.readlines ()