Python module--subprocess

Source: Internet
Author: User
Tags stdin terminates

The subprocess module is used to manage child processes, can call external commands as child processes, and can connect to the Input/output/error pipeline of child processes to get related return information. Like the Linux process, a process can fork a child process and let the child process exec another program. In Python, we fork a child process through the subprocess package in the standard library and run an external program.

Common methods
    • Subprocess.call ()

The parent process waits for the child process to complete, executes the command, returns the execution state, cannot get the contents of the command output, and when the shell is true, the first parameter can enter the command directly, and if False, the list must be used to enter

1 Import subprocess 2 3 ret=subprocess.call ("ipconfig", shell=True)4 ret2= Subprocess.call (["ls""-al"],shell=False )56print(ret)
Pager
    • Subprocess.check_call ()

The parent process waits for the child process to complete, executes the command, cannot get the contents of the command output, if the execution status code is 0, then returns 0, otherwise throws the exception, when the shell is true, the first parameter can enter the command directly, if False, you must use the list to enter

1 Importsubprocess2 3Ret=subprocess.check_call ("ifconfig", shell=True)4Ret2=subprocess.check_call (["ls","-al"],shell=False)5 6 Print("*************")7 Print(ret, Ret2)
Check_call
    • Subprocess.check_output ()

The parent process waits for the child process to complete, executes the command, and if the status code is 0, returns the execution result or throws the exception

Import subprocessret=subprocess.check_output ("ipconfig", shell=True)  Print("*************")#check_output Returns a byte, converting the string output retstr=str (ret,encoding="gbk")print(RETSTR)
Check_output
    •  Subprocess. Popen (...)

 The above call,check_call,check_output these three functions are popen encapsulation, in order to better use the child process, if the demand for more personalized words only need to use Popen, and the above three is different, Popen created objects, The main program does not wait for the child process to complete, and the Wait () function of the object must be called before it waits for

Parameters:

1 args shell command, which can be a string or sequence type (for example: list, tuple)2BUFSIZE specifies the buffer. 0 unbuffered, 1row buffer, other buffer size, negative system buffering3 stdin, stdout, stderr indicate the standard input, output, and error handles of the program, respectively4 PREEXEC_FN is only valid on UNIX platforms and is used to specify an executable object (callable object) that will be called before the child process is run5 CLOSE_SFS under the Windows platform, if Close_fds is set to true, the newly created child process will not inherit the input, output, and error pipes of the parent process. 6 Therefore, you cannot set Close_fds to true while redirecting the standard input, output, and error (stdin, stdout, stderr) of the child process. 7 Shell when the shell is true, the first parameter can enter the command directly, and if False, you must enter it with a list8 CWD is used to set the current directory of child processes9Env is used to specify environment variables for child processes. If env =None, the environment variables for the child process are inherited from the parent process. TenUniversal_newlines different system line breaks, True-agree to use \ n One Startupinfo and Createionflags are only valid under Windows Awill be passed to the underlying CreateProcess () function to set some properties of the child process, such as: the appearance of the main window, the priority of the process, etc.
View Code

Perform additional operations on the created child process

      • Object. Poll () Check the status of the child process
      • Object. Kill () terminates the child process
      • Object. Send_signal () sends a signal to a child process
      • Object. Terminate () terminates child process

Text control of a child process

      • Stdin
      • StdOut
      • StdErr
1 Importsubprocess2 3Ret=subprocess. Popen ("python", stdin=subprocess. PIPE,4stdout=subprocess. PIPE,5Stderr=subprocess. PIPE,6universal_newlines=True)7Ret.stdin.write ("print (1) \ n")8Ret.stdin.write ("print (one) \ n")9 ret.stdin.close ()Ten  One Print(Ret.stdout.read ()) A ret.stdout.close () - Print("***************") - Print(Ret.stderr.read ()) the ret.stderr.close () -  - ########## #结果 ############## -1 +11 -  +***************
a
1 Importsubprocess2 3Ret=subprocess. Popen ("python", stdin=subprocess. PIPE,4stdout=subprocess. PIPE,5Stderr=subprocess. PIPE,6universal_newlines=True)7Ret.stdin.write ("print (1) \ n")8Ret.stdin.write ("print (one) \ n")9 ret.stdin.close ()Ten #Communicate will place the contents of stdout and stderr in a meta-Zuzhong OneOut_error_list =ret.communicate () A Print(out_error_list) -  - ########## #结果 ############ the('1\n11\n',"')
two

      

Python module--subprocess

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.