PYTHON2/3 Execute external command (LINUX) and program (EXE)--subprocess module subprocess

Source: Internet
Author: User
Tags stdin

The Subprocess module enables Python to execute external commands (Linux) and programs (EXE), and to get the corresponding output results for further processing. In Python3.5, the subprocess module replaces the Os.system, os.spawn*. This module is a good embodiment of the characteristics of Python glue language, enrich the ability of Python expansion, so this article mainly introduces the use of Subprocess module, and briefly explain the differences in PYTHON2/3.

Quick Check Table

Python2

Python3.5

subprocess.call (...)

subprocess.run (...)

subprocess.check_call (...)

subprocess.run (..., check=true)

subprocess.check_output (...)

subprocess.run (..., check=true, stdout=pipe). StdOut

subprocess. Popen (...)

subprocess. Popen (...)

As you can see from the table above, there are three functions for subprocess modules in Python2 call, Check_call, Check_output, and a class Popen, essentially these three functions are based on class Popen encapsulation. It is easier to execute the program and get the output. In Python3.5, three functions can be used with the run function and parameter settings, while these three functions can also be used in Python3.5.

Executing external commands and programs
  1import subprocess  2 subprocess.call (["ls", "-L"])   3 subprocess.call ("ls-l", shell=true)  4 subprocess.call ("ffmpeg.exe-i Test3.mp4 Test.avi", shell=true)

The first two call () executes the command to view the current directory, and the third calls () invokes the Ffmpeg.exe program to transcode the Test3.mp4 and generate Test.avi. The call () function receives a list of Split commands by default, and after execution succeeds, the return value ReturnCode is 0, and the failure returns 1. When the argument is shell=true, it receives the string, runs the shell, interprets the string through the shell, and executes the external command and program.

Add error-Handling execution
  1 subprocess.check_call ("exit 1", shell=true)

The Check_call () function is similar to the call () function usage. However, when the return value is not 0 o'clock, subprocess is thrown. Calledprocesserror exception.

Gets the execution of the output
  1 subprocess.check_output ("ls-t | Head-n 4", shell=true)

The Check_output () function is similar to the Check_call () function usage. However, the output of the execution will be returned to the bytes sequence receive, in the Python3 can be decoded decode () into a string str sequence, and further processing. The sample returns details of the top four files sorted by time in the current directory.

The implementation of demand
  1 child1 = subprocess. Popen ("ls-tstdout=subprocess. PIPE)  2 child2 = subprocess. Popen ("head-n 4stdin=child1.  StdOutstdout=subprocess. PIPE)  3 outs, errs = Child2.communicate ()

The Popen () class creates and executes child processes. Through parameter stdin, stdout, stderr change standard input, standard output, standard error, use subprocess. Pipe initialization parameters, create buffer, pass text stream, realize Shell pipeline function. Blocks the parent process through the communicate () method, gets the result of the child process execution, and returns the output and the wrong tuple to the bytes sequence.

You can also use the following methods to manipulate sub-processes to obtain relevant information:

    • Popen.wait (): Blocks the parent process and waits for the child process to terminate. Returns the property ReturnCode.
    • Popen.poll (): Checks the status of the child process. Returns the property ReturnCode.
    • Popen.kill (): Kills the child process.
    • Popen.pid: Gets the process ID of the child process.
    • Popen.returncode: Gets the return code of the child process.

Official documents

The subprocess in Python3

The subprocess in Python2

PYTHON2/3 Execute external command (LINUX) and program (EXE)--subprocess module subprocess

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.