os.system(): Output the result to the screen and return the status of the output command. A result of 0 means the output is correct
os.popen() saves the output results
import subprocess #This module is to replace some old modules, such as os. system, etc., usually better to use under linux
subprocess.call()
The above example shows that if there is no pipeline involved, it can be completed directly in the form of a list, otherwise the shell=True parameter must be added
subprocess.check_call():#Check the return status
subprocess.getstatusoutput()#Return status and result
Three states of subprocess. stdout,stdin,stderr
>>>res=subprocess.Popen("ifconfig|grep192",shell=True,stdout=subprocess.PIPE,stderr=subprocess. PIPE,stdin=subprocess.PIPE)
>>> res.stdout.read()
'inet addr:192.168.1.210 Bcast:192.168.1.255 Mask:255.255.255.0 \n'
For the above command, to read the result, you have to use the res.stdout.read() format
You can also read the error
res.poll () can return the status, 0 means the command is executed correctly
res.terminate() can kill the res process
In the following sentence, you can add cwd : used to set the current directory of the subprocess, env is used to set the environment of the subprocessVariables
##>>>res=subprocess.Popen("sleep6;echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE,cwd=”/tmp”)
The above is the detailed content of Detailed introduction to subprocess module. For more information, please follow other related articles on the PHP Chinese website!