Python supprocess Module

Source: Internet
Author: User

When we need to call the system's command, the first thing to consider is the OS module. Use Os.system () and Os.popen () to operate. However, these two commands are too simple to perform complex operations, such as providing input to a running command or reading the output of a command, judging the running state of the command, managing the parallelism of multiple commands, and so on. In this case, the Popen command in Subprocess will be able to perform the operation we need effectively.

The Subprocess module allows a process to create a new child process, connect to the stdin/stdout/stderr of the child process through a pipeline, and get the return value of the child process.

The Subprocess module allows-spawn new processes, connect to their input/output/error pipes, and obtain their retur N Codes.

This module intends to replace several other, older modules and functions, such as:os.system, os.spawn*, os.popen*, popen2.* , commands.*

This module is a class: Popen.

123 #Popen它的构造函数如下: subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None,stderr=None, preexec_fn=None, close_fds=False, shell=False,<br>                 cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
parameterSimple command:
123456789101112131415161718 importsubprocessa=subprocess.Popen(‘ls‘)#  创建一个新的进程,与主进程不同步 print(‘>>>>>>>‘,a)#a是Popen的一个实例对象‘‘‘>>>>>>> <subprocess.Popen object at 0x10185f860>__init__.py__pycache__log.pymain.py‘‘‘# subprocess.Popen(‘ls -l‘,shell=True) # subprocess.Popen([‘ls‘,‘-l‘])
Subprocess. PIPE

When you create a Popen object, subprocess. Pipe can initialize stdin, stdout, or stderr parameters. Represents a standard stream that communicates with a child process.

123456 importsubprocess # subprocess.Popen(‘ls‘)p=subprocess.Popen(‘ls‘,stdout=subprocess.PIPE)#结果跑哪去啦?print(p.stdout.read())#这这呢:b‘__pycache__\nhello.py\nok.py\nweb\n‘

This is because subprocess created the child process, the result of which in the child process, if want to execute the result to go to the main process, you need a pipeline, namely: Stdout=subprocess. PIPE

Subprocess. STDOUT

When you create a Popen object, the stderr parameter is initialized to indicate that the error is output through the standard output stream.

Methods of PopenView CodeTool functions for Supprocess modules
12345678910111213 supprocess模块提供了一些函数,方便我们用于创建进程来实现一些简单的功能。subprocess.call(*popenargs, **kwargs)运行命令。该函数将一直等待到子进程运行结束,并返回进程的returncode。如果子进程不需要进行交 互,就可以使用该函数来创建。subprocess.check_call(*popenargs, **kwargs)与subprocess.call(*popenargs, **kwargs)功能一样,只是如果子进程返回的returncode不为0的话,将触发CalledProcessError异常。在异常对象中,包 括进程的returncode信息。check_output(*popenargs, **kwargs)与call()方法类似,以byte string的方式返回子进程的输出,如果子进程的返回值不是0,它抛出CalledProcessError异常,这个异常中的returncode包含返回码,output属性包含已有的输出。getstatusoutput(cmd)/getoutput(cmd)这两个函数仅仅在Unix下可用,它们在shell中执行指定的命令cmd,前者返回(status, output),后者返回output。其中,这里的output包括子进程的stdout和stderr。
DemoInteractive commands:

The commands for terminal input are divided into two types:

    • Input can be output, such as: ifconfig
    • Input to an environment that relies on re-entry, such as: Python

Python supprocess Module

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.