Detailed introduction to subprocess module

高洛峰
Release: 2017-03-15 15:39:56
Original
1854 people have browsed it

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

Detailed introduction to subprocess module

os.popen() saves the output results

Detailed introduction to subprocess module

import subprocess #This module is to replace some old modules, such as os. system, etc., usually better to use under linux

subprocess.call()

Detailed introduction to subprocess module

Detailed introduction to subprocess module

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

Detailed introduction to subprocess module

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

Detailed introduction to subprocess module

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!