Overview
Consider such a problem, there is hello.py script, output "Hello, world!" ; There is a testinput.py script that waits for user input and then prints the data entered by the user. So, how to send hello.py output to testinput.py, and finally testinput.py print received "Hello, world!". Let me step through the way the shell interacts.
The hello.py code is as follows:
Copy CodeThe code is as follows:
#!/usr/bin/python
Print "Hello, world!"
The testinput.py code is as follows:
Copy CodeThe code is as follows:
#!/usr/bin/python
str = raw_input ()
Print ("Input string is:%s"% str)
1.os.system (CMD)
This method simply executes the shell command, returning a return code (0 means the execution succeeds or fails)
Copy CodeThe code is as follows:
Retcode = Os.system ("Python hello.py")
Print ("Retcode is:%s"% retcode);
Output:
Copy CodeThe code is as follows:
Hello, world!.
Retcode is:0
2.os.popen (CMD)
Executes the command and returns the input stream or output stream of the executing command program. This command only operates one-way streams, interacts with shell commands one-way, and cannot interact in two directions.
Returns the program output stream, connected to the output stream with a fouput variable
Copy CodeThe code is as follows:
Fouput = Os.popen ("Python hello.py")
result = Fouput.readlines ()
Print ("Result is:%s"% result);
Output:
Copy CodeThe code is as follows:
Result is: [' Hello, world!\n ']
Returns the input stream, connected to the output stream with a finput variable
Copy CodeThe code is as follows:
Finput = Os.popen ("Python testinput.py", "W")
Finput.write ("How is you\n")
Output:
Copy CodeThe code is as follows:
Input string is:how is you
3. Using the Subprocess module
Subprocess.call ()
Like Os.system (), note that the "shell=true" here means executing the command with the shell instead of the default OS.EXECVP ().
Copy CodeThe code is as follows:
F = Call ("Python hello.py", shell=true)
Print F
Output:
Copy CodeThe code is as follows:
Hello, world!.
Subprocess. Popen ()
The use of Popen can be a bidirectional flow of communication, you can send a program's output stream to another program's input stream.
Popen () is a constructor for the Popen class, and communicate () returns a tuple (Stdoutdata, stderrdata).
Copy CodeThe code is 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
#print P2.stdout.readlines ()
Output:
Copy CodeThe code is as follows:
Input string Is:hello, world!
The integration code is as follows:
Copy CodeThe code is 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 is:%s"% result);
Finput = Os.popen ("Python testinput.py", "W")
Finput.write ("How is 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
#print P2.stdout.readlines ()
3 ways to interact with the shell and Python