Python uses subprocess to call external commands

Source: Internet
Author: User
Tags response code

Starting from Python 2.4, Python introduced the subprocess module to Manage Sub-processes to replace the methods of some old modules:OS. System,OS. Spawn,OS. popen,Popen2 .*,Commands .*.SubprocessNot only can an external command be called as a sub-process, but also can be connected to the input/output/error pipeline of the sub-process to obtain the relevant response information.

Use subprocess Module

Use popen class

class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

ARGs parameter, which specifies the external program to be executed. The value can be a string or a sequence.

Shell is false by default.

In UNIX, when shell = false, popen calls OS .exe CVP () to execute ARGs
When shell = true, if ARGs is a string, popen directly calls the system shell to execute the program specified by args. If
ARGs is a sequence. The first item of argS is to define the program command string. Other items are additional parameters used to call the system shell.

In Windows, popen calls CreateProcess () to execute ARGs regardless of the shell value.
The specified external program. If ARGs is a sequence, first convert list2domainline () to a string, but note that it is not a MS windows
All programs can use list2cmdline to convert to a command line string.

Stdin, stdout, and stderr are used to specify standard input, output, and incorrect handle of a program. The value can be pipe, file descriptor, file object, and none.

Take the ping command in Linux as an example:

pingPopen = subprocess.Popen(args='ping -c4 www.google.cn', shell=True)

To obtain the ping output information:

pingPopen = subprocess.Popen(args='ping -c4 www.google.cn', shell=True, stdout=subprocess.PIPE)print pingPopen.stdout.read()

An external program is executed in a sub-process. To wait for the end of the process, you can use wait ():

pingPopen.wait()

The wait () method returns a response code.

Or call communicate () after creating a popen object ():

stdReturn = subprocess.Popen(args='ping -c4 www.google.cn', shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()

Communicate () returns one (stdout, sterr ).

Use call () and check_all ()

The Subprocess module has two convenient functions: Call () and check_all (), which can be called directly to execute external programs.

call(*popenargs, **kwargs)
check_call(*popenargs, **kwargs)

Their parameter list is the same as the constructor parameter list in popen. Returns a returncode. For example:

subprocess.call('ping -c4 www.google.cn',shel=True)
References
  • Http://www.python.org/doc/2.5.2/lib/module-subprocess.html
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.