Introduction to Python and shell, pythonshell

Source: Internet
Author: User

Introduction to Python and shell, pythonshell

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:
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 only executes the shell command and returns a return code (0 indicates that the execution is successful, otherwise it indicates that the execution 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)

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.
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 and connects to the output stream using the finput variable.
Copy codeThe Code is as follows:
Finput = OS. popen ("python TestInput. py", "w ")
Finput. write ("how are you \ n ")

Output:
Copy codeThe 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.
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 ()

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 ).
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 way
# 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 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 ()

Related Article

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.