Python Learning subprocess Module

Source: Internet
Author: User

Subprocess. Popen

This module mainly provides a class Popen:

            Executable=none,      stdin=none,       creationflags=0)  

This pile of stuff is so crazy:

1 row buffer
Other positive buffer size
negative value with default system buffering (generally full buffering)

generally not, args string or list the first item represents the program name

stdin
stdout
stderr

none no redirection, inherit parent process
Pipe creation pipeline
File Object
File descriptor (integer)
StdErr can also be set to STDOUT

hook function, executed between fork and exec. (Unix)

unix whether to close files other than 0/1/2 before executing the new process
Windows does not inherit or inherit the file descriptor of the parent process

Unix is equivalent to args before adding   "/bin/sh"   "-C"
window, equivalent to adding "cmd.exe /c"

Cwd

Set up working directory

Env

Setting environment variables

window passed to CreateProcess structure

Creationflags

Under Windows, pass Create_new_console Create your own console window

    • The most disturbing thing was the args parameter. Can be a string, which can be a list.

Subprocess. Popen (["Gedit", "Abc.txt"])subprocess. Popen ("Gedit abc.txt")

These two, the latter will not work. Because if it is a string, it must be the path of the program. (Consider the UNIX API function exec, which accepts a list of strings)

    • But the following can work

Subprocess. Popen ("Gedit abc.txt", shell=true)

This is because it is equivalent to

Subprocess. Popen (["/bin/sh", "-C", "Gedit Abc.txt"])

is a parameter of sh, it doesn't matter.

    • Under Windows, the following can work

Subprocess. Popen (["Notepad.exe", "Abc.txt"])subprocess. Popen ("notepad.exe abc.txt")

This is because the API function under Windows CreateProcess accepts a string. Even parameters in the form of a list need to be merged into a string before being passed to the API function.

    • Like above

Subprocess. Popen ("notepad.exe abc.txt" shell=true)

Equivalent to

Subprocess. Popen ("cmd.exe/c" + "notepad.exe abc.txt" shell=true)
subprocess.call*

The module also provides several convenience functions (which in itself is a good example of popen use)

    • Call () executes the program and waits for it to complete

def call (*popenargs, **kwargs):    return Popen (*popenargs, **kwargs). Wait ()
    • Check_call () calls the preceding call and throws an exception if the return value is nonzero

def check_call (*popenargs, **kwargs):    retcode = Call (*popenargs, **kwargs)    if retcode:        cmd = Kwargs.get ("args")        raise Calledprocesserror (Retcode, cmd) return 0    
    • Check_output () executes the program and returns its standard output

def check_output (*popenargs, **kwargs):    process = Popen (*popenargs, Stdout=pipe, **kwargs)    output, Unused_err = Process.communicate ()    retcode = Process.poll ()    if retcode: cmd = Kwargs.get ("args") Raise Calledprocesserror (Retcode, cmd, output=output) return output      
Popen Object

This object provides a number of method functions available. And it's been used before. Wait ()/poll ()/communicate ()

Poll ()

Check if end, set return value

Wait ()

Wait for end, set return value

communicate ()

parameter is standard input, return standard output and standard error

send_signal ()

send signal (mostly useful under Unix)

terminate ()

terminate process, UNIX corresponding sigterm signal, Windows downgrade API function TerminateProcess ()

kill ()

kill process (Unix corresponds to Sigkill signal), Windows below

Stdin
StdOut
StdErr

parameter, it is useful to specify pipe in the

Pid

Process ID

ReturnCode

Process return value

Reference

Python Learning subprocess 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.