subprocess module of the Python standard library series
This module allows the spawn processes, connect to their input/output/error pipes, and obtain their return codes.
Examples of common methods
Call ()
Executes the command and returns the status code, indicating that the command 0
execution was successful, and that the command execution was unsuccessful.
>>> ret = Subprocess.call (["LS", "-l"], Shell=false) Total 4-rw-r--r--1 root root 172, 21:21 file.conf>& Gt;> ret0
Another way of doing it
# shell=true means call Native shell command to execute >>> ret = Subprocess.call ("Ls-l", Shell=true) Total 4-rw-r--r--1 root root 172 21:21 file.conf>>> Ret0
Check_call ()
Executes the command, returns 0 if the execution status code is 0, or throws an exception
# Execute a correct command will return execution result and status code >>> subprocess.check_call (["LS", "-l"]) total 4-rw-r--r--1 root root 172 may 21:21 file . conf0# If you are performing an incorrect command, you will be returned with an error message >>> subprocess.check_call (["LS", "a"]) Ls:cannot access a:no such file or dire Ctorytraceback (most recent): File ' <stdin> ', line 1, in <module> file "/usr/lib64/python2.6/subpr ocess.py ", line 505, in Check_call raise Calledprocesserror (Retcode, cmd) subprocess. Calledprocesserror:command ' [' ls ', ' a '] ' returned Non-zero exit status 2
Check_output ()
Executes the command, if the status code is 0, returns the execution result, otherwise throws an exception
# execution succeeds assigning the result of execution to the variable v>>> V = Subprocess.check_output ("Python-v", shell=true) # The command that executes the error will output an exception >>> Subprocess.check_output ("Pasas", shell=true) ' Pasas ' is not an internal or external command, nor is it a program or batch file that can be run. Traceback (most recent): File ' <stdin> ', line 1, in <module> file ' C:\Python35\lib\subprocess.py ', Line 629, in Check_output **kwargs). StdOut File "C:\Python35\lib\subprocess.py", line 711, in run Output=stdout, s Tderr=stderr) subprocess. Calledprocesserror:command ' Pasas ' returned Non-zero exit status 1
The above three modes of execution, when executing the command,shell
Default equalsTrue
EqualsTrue
, the command inside the parentheses is a row, ifshell
EqualsFalse
So[]
The string inside is an element of the command, and when executed, the[]
Strings inside the string are stitched together to execute.
Subprocess. Popen ()
call()
、check_call()
、check_output()
The default internal calls aresubprocess.Popen()
, whilesubprocess.Popen()
is used to perform more complex system commands.
Parameters
Parameters |
Description |
Stdin |
Standard input |
StdOut |
Standard output |
StdErr |
Error handle |
Cwd |
Used to set the current directory of the child process |
Env |
The environment variable used to specify the child process. If env = None, the environment variables of the child process are inherited from the parent process |
Execute common commands
>>> subprocess. Popen ("Python-v", Shell=true) <subprocess. Popen object at 0x0000025c97233c88># python 3.5.1 is output result >>> python 3.5.1
There are two types of execution commands:
Input can be output, such as: ifconfig
Input for an interactive environment, relying on re-entry, such as: Python
>>> import subprocess# First go to the '/tmp ' directory and then create the subprocess folder, Shell=true >>> subprocess. Popen ("MkDir subprocess", shell=true, cwd= '/tmp ',) <subprocess. Popen object at 0x7f267cc3d390>>>> import os>>> os.system ("ls/tmp") subprocess
Subprocess. Popen () instance
# Importing Subprocess module import subprocess# Execute python command, enter Python interpreter, stdin standard input, stdout standard output, stderr error output, universal_newlines= True to automatically enter a newline character, obj = subprocess. Popen (["Python"], stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Stderr=subprocess. PIPE, Universal_newlines=true) # Executes the standard input, the write is followed by the input command Obj.stdin.write ("print (1) \ n") Obj.stdin.write ("Print (2)") # After the input is turned off obj.stdin.close () # reads the contents of the standard output, assigns a value to the Cmd_out object cmd_out = Obj.stdout.read () # Turns off the standard output obj.stdout.close () # reads the contents of the error output, Assign to Cmd_error object cmd_error = Obj.stderr.read () # Turn off error Output obj.stderr.close () # Output content print (cmd_out) print (cmd_error)
Execution results
c:\python35\python.exe f:/python_code/sublime/week5/day02/sub.py12process finished with Exit code 0
# Importing Subprocess module import subprocess# Execute python command, enter Python interpreter, stdin standard input, stdout standard output, stderr error output, universal_newlines= True to automatically enter a newline character, obj = subprocess. Popen (["Python"], stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Stderr=subprocess. PIPE, Universal_newlines=true) # executes two commands obj.stdin.write ("print (1) \ n") Obj.stdin.write ("Print (2)") # Communicate the error output or the content of the standard output to the Out_error_list object, if there is an error on the assignment error output, otherwise copy the standard output out_error_list = obj.communicate () # Output Out_ Content of the Error_list object print (out_error_list)
Execution results
c:\python35\python.exe f:/python_code/sublime/week5/day02/sub.py (' 1\n2\n ', ') Process finished with exit code 0
# Importing Subprocess module import subprocess# Execute python command, enter Python interpreter, stdin standard input, stdout standard output, stderr error output, universal_newlines= True to automatically enter a newline character, obj = subprocess. Popen (["Python"], stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Stderr=subprocess. PIPE, Universal_newlines=true) # Executes the print ("Hello") command directly, then assigns the error or the correct result to the Out_error_list object out_error_list = Obj.communicate (' Print (' "Hello ') ') # outputs the contents of the Out_error_list object print (out_error_list)
Execution results
C:\Python35\python.exe f:/python_code/sublime/week5/day02/sub.py (' hello\n ', ') Process finished with exit code 0
#Python标准库 #Subprocess
subprocess module of 12Python standard Library series