[Python] subprocess Module

Source: Internet
Author: User

Subprocess.Call(ARGs,*,
Stdin = none,Stdout = none,Stderr = none,Shell = false)

In the Internal call function, all parameters cannot be passed.

ClassSubprocess.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)

First ParameterARGsIt can be a python sequence or a simple string

Generally, sequence is recommended, because various quotation marks and spaces can be processed, such as escaping arguments.

Providing a sequence of arguments is generallypreferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names ).

However, if shell = true is specified, all parameters must be included in a single string, such as "ls dirname ", otherwise, python will execute/bin/sh-C ls dirname. That is, dirname is changed to the parameter passed to/bin/sh, but not to LS.

IfShellIsTrue, It isrecommended to passARGsAs a string rather than as a sequence.

IfARGsIs a sequence, the first item specifies the command string, andany additional items will be treated as additional arguments to the shellitself. That is to say,Popen
Does the equivalent:

Popen(['/Bin/Sh', '-C', ARGs[0], ARGs[1], ...])

If the command to be executed has a long string of complex parameters, it will be very troublesome to break it into a single sequence element. AvailableShlex. Split ()To complete

In sequence mode, a single element cannot contain spaces.["Ls", "-l"]Yes,["Ls", "-l"]Error!

Note: The above sequence concepts were incorrectly understood in the past two days. There are 7 sequence types in Python, which can be strings or integers.

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.