Introduction to three ways of interaction between Python and shell

Source: Internet
Author: User
This article mainly introduces three ways of interaction between Python and shell. This article describes three methods, including OS. system, OS. popen, and subprocess modules. For more information, see Overview

To solve this problem, we have a hello. py script that outputs "hello, world !"; There is a TestInput. py script, waiting for user input, and then printing user input data. Then, how to send the hello. py output content to TestInput. py, and finally TestInput. py prints the received "hello, world !". Next I will explain the shell interaction method step by step.

The hello. py code is as follows:

The code is as follows:


#! /Usr/bin/python
Print "hello, world! "


The TestInput. py code is as follows:

The code is as follows:


#! /Usr/bin/python
Str = raw_input ()
Print ("input string is: % s" % str)


1. OS. system (cmd)

This method only executes the shell command and returns a return code (0 indicates that the execution is successful, otherwise it indicates that the execution fails)

The code is as follows:


Retcode = OS. system ("python hello. py ")
Print ("retcode is: % s" % retcode );


Output:

The code is as follows:


Hello, world!
Retcode is: 0


2. OS. popen (cmd)

Execute the command and return the input stream or output stream of the command execution program. this command can only operate on one-way streams, one-way interaction with shell commands, not two-way interaction.

Returns the program output stream and uses the fouput variable to connect to the output stream.

The code is as follows:


Fouput = OS. popen ("python hello. py ")
Result = fouput. readlines ()
Print ("result is: % s" % result );

Output:

The code is as follows:


Result is: ['Hello, world! \ N']

Returns the input stream and connects to the output stream using the finput variable.

The code is as follows:


Finput = OS. popen ("python TestInput. py", "w ")
Finput. write ("how are you \ n ")


Output:

The code is as follows:


Input string is: how are you

3. use the subprocess module

Subprocess. call ()

Kerberos.system({, here the zookeeper true=command is executed with shell, but not with the same OS .exe cvp.

The code is as follows:


F = call ("python hello. py", shell = True)
Print f

Output:

The code is as follows:


Hello, world!

Subprocess. Popen ()

Popen can be used to implement bidirectional stream communication and send the output stream of a program to the input stream of another program.
Popen () is the Popen class constructor. communicate () returns the tuples (stdoutdata, stderrdata ).

The 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 way
# Print p2.stdout. readlines ()

Output:

The code is as follows:


Input string is: hello, world!

The integration code is as follows:

The 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 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.