The Subprocess module is a module introduced by Python starting with version 2.4. It is mainly used to replace some old module methods, such as Os.system, os.spawn*, os.popen*, commands.* and so on. subprocess executes the external instruction through the child process and obtains the return information of the child process's execution through the Input/output/error pipeline.
Common methods:
Subprocess.call (): Executes the command and returns the execution state, where the command needs to be passed in as a list when the shell parameter is false, and the command can be passed directly when the shell is true
Examples are as follows:
>>> a = Subprocess.call ([‘df ", ' -ht '],shell=false) Filesystem Type Size used Avail use%< Span style= "COLOR: #000000" > mounted on/dev/sda2 ext4 94G 64G 26G 72%/tmpfs tmpfs 2.8G 0 2.8 G 0%/dev/shm/dev/sda1 ext4 976M 56M 853M 7%/boot>> > A = Subprocess.call ( ' df-ht ", Shell=true) Filesystem Type Size used Avail Use% mounted on/dev/sda2 ext4 94G 64G 26G 72%/tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm/dev/sda1 ext4 976M 56M 853M 7%/boot
>>> Print a
0
Subprocess.check_call (): usage is similar to Subprocess.call (), except that when the return value is not 0 o'clock, the exception is thrown directly
Example:
>>> A = Subprocess.check_call (‘Df-ht', shell=True) Filesystem Type Size used Avail use%Mounted On/dev/sda2 ext4 94G 64G 26G 72%/Tmpfs tmpfs 2.8G 0 2.8G 0%/dev/SHM/DEV/SDA1 ext4 976M 56M 853M 7%/Boot>>>PrintA0>>> A = Subprocess.check_call (‘Dfdsf ", Shell=true)/bin/sh:dfdsf:command not Foundtraceback (most recent call last): File <stdin>in <module> File "/usr /lib64/python2.6/subprocess.pyin Check_call raise Calledprocesserror ( Retcode, cmd) subprocess. Calledprocesserror:command ' DFDSF "returned Non-zero exit status 127
Subprocess.check_output (): usage is similar to the above two methods, except that if the return value is 0 o'clock, the output is returned directly, and if the return value is not 0, the exception is thrown directly. It is necessary to note that this method is only in python3.x.
Subprocess. Popen ():
In some complex scenarios, we need to take the execution output of one process as input to another process. In other scenarios, we need to go into an input environment and then execute a series of instructions. At this point we need to use the Popen () method to suprocess. The method has the following parameters:
The Args:shell command, which can be a string, or a sequence type, such as List,tuple.
BufSize: Buffer size, can not care
Stdin,stdout,stderr: Indicates the standard input, standard output and standard error of the program, respectively
Shell: Same as used in the previous method
CWD: Used to set the current directory of child processes
ENV: The environment variable used to specify the child process. If env=none, the environment variable is inherited from the parent process by default
Universal_newlines: Different line breaks for different systems, when this argument is set to true, means \ n is used as a newline character
Example 1, create a suprocesstest directory under/root:
>>> a = subprocess. Popen ('mkdir subprocesstest', shell=true,cwd='/root')
Example 2, use Python to execute several commands:
ImportSubprocessobj = subprocess. Popen (["Python"], stdin=subprocess. PIPE, Stdout=subprocess. PIPE, stderr=Subprocess. PIPE) Obj.stdin.write ( ' print 1 \n ) obj.stdin.write ( ' print 2 \n ' ) obj.stdin.write ( ' print 3 \n ' ) obj.stdin.write ( ' print 4 \n" ) obj.stdin.close () cmd_out = Obj.stdout.read () Obj.stdout.close () cmd_error = Obj.stderr.read () obj.stderr.close () print Cmd_outprint cmd_error
You can also use the following methods:
ImportSubprocessobj = subprocess. Popen (["Pythonsubprocess. PIPE) obj.stdin.write ( ' print 1 \ n ' ) obj.stdin.write (print 2 \n ") Obj.stdin.write ( ' print 3 \n ) obj.stdin.write ( ' print 4 \n ' ) out_error_list = Obj.communicate () Span style= "COLOR: #0000ff" >print out_error_list
Example 3, the output of one child process as input to another child process:
Import subprocesschild1 = subprocess. Popen (["cat","/etc/passwd"], stdout=subprocess. PIPE) child2 = subprocess. Popen (["grep","0:0"],stdin=child1.stdout, stdout=subprocess. PIPE) out = Child2.communicate ()
Other methods:
Import subprocesschild = subprocess. Popen ('sleep', # # # # # # # # # # # # # # # # # shell=true,stdout=
Python's subprocess module