转载原文出处:http://mushanblog.com/blog/python-subprocess-module-primer
自己又结合源码和说明文档
有时候我们想做python脚本里调用其他程序怎么做?或者想获得某个shell命令返回的结果怎么做?答案是subprocess。
subprocess模块用来建立子进程,并且可以控制子进程的stdout,stdout,stderr,获取子进程的返回码。在脚本中通过subprocess来调用外部程序或者shell命令,会让你的脚本如虎添翼无所不能。
可能你之前知道python调用外部程序的函数,比如os.system,但是,那些都过时了!subprocess是python2.4加入的一个模块,是用来取代其他的创建子进程的函数和模块:
- os.system
- os.spawn*
- os.popen*
- popen2.*
- commands.*
This module allows you to spawn processes, connect to their
input/output/error pipes, and obtain their return codes. This module
intends to replace several other, older modules and functions, like:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
input/output/error pipes, and obtain their return codes. This module
intends to replace several other, older modules and functions, like:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
subprocess模块只有一个Popen类,所有的操作都可以通过Popen类来实现。同时subprocess还定义了一些函数,这些函数都是对Popen类的包装,方便我们使用的。如果这些函数不能满足你,再去定制Popen类吧。
This module defines one class called Popen:
This module also defines some shortcut functions:
call(*popenargs, **kwargs):
Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
运行args指定的命令,返回returncode。
check_call(*popenargs, **kwargs):
Run command with arguments. Wait for command to complete. If the
exit code was zero then return, otherwise raise
CalledProcessError. The CalledProcessError object will have the
return code in the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
check_call(["ls", "-l"])
check_call(*popenargs, **kwargs):
Run command with arguments. Wait for command to complete. If the
exit code was zero then return, otherwise raise
CalledProcessError. The CalledProcessError object will have the
return code in the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
check_call(["ls", "-l"])
check_call的参数与call一致。在call的基础上,check_call在returncode不等于0时,会抛出subprocess.CalledProcessError异常。
check_output(*popenargs, **kwargs):
Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
output = check_output(["ls", "-l", "/dev/null"])
check_output(*popenargs, **kwargs):
Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
output = check_output(["ls", "-l", "/dev/null"])
check_output是非常有用的函数。因为他返回的是stdout的内容。同样,check前缀表示如果returncode非零,将抛出subprocess.CalledProcessError异常。
主要API就是一个Popen类和三个函数(call(),check_call(),check_output())以及一个异常类CalledProcessError,关于API的详细说明见转载原文,讲得很好哟~~