一、subprocess标准库

  python执行shell脚本,通常会使用so模块中的几个方法,如system、spawn*、popen等。subprocess标准库的引入就是为了取代这些老的模块方法。subprocess使用时,父进程创建子进程去执行一个外部程序,并提供了标准输入输出和管道(pipe)的实现方法。

subprocess.Popen()

  subprocess库的一个基础类,下文介绍的subprocess.run、subprocess.call、subprocess.check_call、subprocess.check_output都是在此基础上的封装,若要实现一些复杂的业务流程使用subprocess.Popen()更灵活。

  程序运行subprocess.Popen()类,父进程创建子进程后,不会等待子进程执行完成。如果需要等待子进程,需要加入wait()方法阻塞父进程。

示例1

child = subprocess.Popen('ping www.baidu.com')
print('End')

示例2

child = subprocess.Popen('ping www.baidu.com')
child.wait()
print('End')

以上示例1没有等child 执行完就print,示例2父进程先阻塞,等待child执行完再print。

subprocess.Popen(args, bufsize=-1, executable=None,stdin=None, stdout=None, stderr=None,preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,shell=False, cwd=None, env=None, universal_newlines=False,startupinfo=None, creationflags=0,restore_signals=True, start_new_session=False,pass_fds=(), *, encoding=None, errors=None)

args:shell命令,可以是str类型,或者list和tuple

bufsize:缓冲区

stdin、stdout、stderr:标准输入输出和标准错误日志

subprocess.PIPE:创建Popen对象时,subprocess.PIPE可以初始化stdin、stdout、stderr参数。

标准输出:

import subprocess

child = subprocess.Popen(['shell','python --version'], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
shell_out = child.stdout.read()
# shell_out = child.communicate() 也可以使用communicate()方法输出
shell_error = child.stderr.read()
print(shell_out)
print(shell_error)

其他方法:

child.poll()    #检查子进程状态

child.kill()     #终止子进程

child.send_signal()  #向子进程发送信号

child.terminate()   #终止子进程

subprocess.run()

subprocess.run()函数是Python3.5中新增的一个高级函数,其返回值是一个subprocess.CompletedPorcess类的实例。

child = subprocess.run('python --version', shell =True)
print(child) #CompletedProcess(args='python --version', returncode=0)

subprocess.call()

父进程等待子进程完成

执行成功返回0

执行失败returncode=2,不会主动抛error

child = subprocess.call('python --version', shell =True)
print(child) #

subprocess.check_call()

父进程等待子进程完成

执行成功返回0

returncode不为0,抛出subprocess.CalledProcessError:error信息只有returncode

child = subprocess.check_call('python --version', shell =True)
print(child) #

subprocess.check_output()

父进程等待子进程完成

执行成功返回output信息

returncode不为0,抛出subprocess.CalledProcessError:error信息包含returncode和output信息

child = subprocess.check_output('python --version', shell =True)
print(child) #b'Python 3.6.4\r\n'

python subprocess模块详解的更多相关文章

  1. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  2. python docopt模块详解

    python docopt模块详解 docopt 本质上是在 Python 中引入了一种针对命令行参数的形式语言,在代码的最开头使用 """ ""&q ...

  3. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  4. python pathlib模块详解

    python pathlib模块详解    

  5. Python Fabric模块详解

    Python Fabric模块详解 什么是Fabric? 简单介绍一下: ​ Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...

  6. python time 模块详解

    Python中time模块详解 发表于2011年5月5日 12:58 a.m.    位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...

  7. python之subprocess模块详解--小白博客

    subprocess模块 subprocess是Python 2.4中新增的一个模块,它允许你生成新的进程,连接到它们的 input/output/error 管道,并获取它们的返回(状态)码.这个模 ...

  8. python常用模块详解

    python常用模块详解 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用p ...

  9. subprocess模块详解

    subprocess是Python与系统交互的一个库,该模块允许生成新进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码. 该模块旨在替换几个较旧的模块和功能: os.system os.s ...

随机推荐

  1. 转载-js如何设置网页横屏和竖屏切换

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. Pycharm配置

    平台:win10 x64 Pycharm的下载,安装,破解,编辑字体+配置IDE 详见博客:https://blog.csdn.net/yctjin/article/details/70307933? ...

  3. Android Studio开发环境搭建和HelloWorld

    跟着教程做的,已经有了JDK,直接进行后面的步骤,下载安装Android SDK 没有FQ,教程里的网址打不开,就换了个.网址 http://tools.android-studio.org/inde ...

  4. python 更换 版本

    这是一个悲伤的安装ipython的过程. 写下来留个教训吧. 也是希望对博友一些帮助吧. 注: 我也写了一篇window下安装bpython的文章(个人感觉bpython要比ipython强大的多), ...

  5. 【慕课网实战】Spark Streaming实时流处理项目实战笔记十八之铭文升级版

    铭文一级: 功能二:功能一+从搜索引擎引流过来的 HBase表设计create 'imooc_course_search_clickcount','info'rowkey设计:也是根据我们的业务需求来 ...

  6. nginx 502错误 upstream sent too big header while reading response header from upstream

    原本的设置是 proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; 在这种配置下,使用fiddler进行抓包分 ...

  7. ReactNative学习笔记(六)集成视频播放

    概述 视频播放可以自己写原生代码实现,然后注入JS.如果对视频播放没有特殊要求的话,可以直接使用现成插件. 到官方推荐的插件网站搜索找到下载量第一的插件:react-native-video. 安装 ...

  8. 项目Alpha冲刺(团队3/10)

    项目Alpha冲刺(团队3/10) 团队名称: 云打印 作业要求: 项目Alpha冲刺(团队) 作业目标: 完成项目Alpha版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 ...

  9. 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别一览

    平时,不管在pc端页面还是移动端页面,因为我们一般很少会设置某个块的的高度,但是呢,我有时候有需要取到这些高度以便于我们方便进行判断和下一步的编写.一般这个时候我都是直接的获取一个块的高度.heigh ...

  10. LabVIEW(十五):右键菜单添加创建VI模版

    如果在项目研究中使用到的某一个模版文件次数较多,可以单独为某一个模版文件新建右键选项.以文本格式打开注册表,添加的右键内容即为Data后面的内容.Reg内容不可手动修改,可以通过LabVIEW的编程实 ...