subprocess模块

一 功能:

*spawn new processes,connect to their input/output/error pipes, and obtain their return codes.

*intends to replace several older modules and functions:os.system,os.popen 等等.


二 subprocess模块属性:

(1)call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

*Run the command described by args. Wait for command to complete, then return the returncode attribute.

*Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.


(2)check_all(args, *, stdin=None, stdout=None, stderr=None, shell=False)

*Run command with arguments. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute.


(3)check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

*Run command with arguments and return its output as a byte string.

*If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.


(4)PIPE:

*Special value that can be used as the stdin, stdout or stderr argument to Popen and indicates that a pipe to the standard stream should be opened.


(5)STDOUT

*Special value that can be used as the stderr argument to Popen and indicates that standard error should go into the same handle as standard output.



(6)exception subprocess.CalledProcessError
*Exception raised when a process run by check_call() or check_output() returns a non-zero exit status.

(7)Popen:下节介绍。


三 Popen类方法:

(1)Popen.poll():

*Check if child process has terminated. Set and return returncode attribute.


(2)Popen.wait():

*Wait for child process to terminate. Set and return returncode attribute.


(3)Popen.communicate(input=None):

*Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.

*communicate() returns a tuple (stdoutdata, stderrdata).


(4)Popen.send_signal(signal):

*Sends the signal signal to the child.


(5)Popen.terminate():

*Stop the child. On Posix OSs the method sends SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.



(6)Popen.kill():

*Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().


(7)Popen.returncode
*The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn’t terminated yet.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值