subprogress允许我们创建新进程,进程之间通过stdin,stdout,stderr管道进行通信,该模块自从python2.4版本引入

这个模块是为了替代 os.system os.spawn*这两个模块产生的。也就是说,可以代替shell编写命令行脚本。

run 方法

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly.

推荐使用run方法处理大部分场景,如果有更高级的需求,可以使用底层的Popen接口。

run()方法是python3.5加入的。(怪不得网上的教程都没看到这个方法)

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None)

不过这个模块貌似只适合linux下使用。因为不支持powershell的那些命令(cmd 还是支持的)。windows下只有exe的可执行命令行程序比较适合调用吧。还有一点是,命令执行结果返回0和1,这点就和linux的命令式一致的。

run方法包含了大部分最常用的参数,而且很多接口参数都是和Popen一致

class subprocess.CompletedProcess

subprocess.CompletedProcess 是run方法的返回值

包含以下参数:

The return value from run(), representing a process that has finished.

args

The arguments used to launch the process. This may be a list or a string.

returncode

Exit status of the child process. Typically, an exit status of 0 indicates that it ran successfully.

A negative value -N indicates that the child was terminated by signal N (POSIX only).

stdout

Captured stdout from the child process. A bytes sequence, or a string if run() was called with an encoding, errors, or text=True. None if stdout was not captured.

If you ran the process with stderr=subprocess.STDOUT, stdout and stderr will be combined in this attribute, and stderr will be None.

stderr

Captured stderr from the child process. A bytes sequence, or a string if run() was called with an encoding, errors, or text=True. None if stderr was not captured.

check_returncode()

If returncode is non-zero, raise a CalledProcessError.

# win10 1809 python3.7

import subprocess

res = subprocess.run(['ffmpeg','-h'])

# subprocess.CompletedProcess

print('args: ',res.args)

print('stdout: ',res.stdout)

print('stderr: ',res.stderr)

# 当returncode为非零值,抛出错误 CalledProcessError

res.check_returncode()

args: ['ffmpeg', '-h']

stdout: None

stderr: None

subprocess.DEVNULL 应该就是linux里的 /dev/null

subprocess.PIPE 可以提供给stdin,stdout or stderr 参数

exception subprocess.SubprocessError

exception subprocess.TimeoutExpired

。。。

最常用的参数

args 调用的命令,可以是字符串,或者一系列的参数比如列表。

stdin stdout,stderr 程序的标准输入。。。 有效的参数值是PIPE和DEVNULL

text ,开启文本模式,可以用encoding指定编码,stdin等都会用文本形式传递。如果不开启,他们会以二进制流形式传递

在python3.7 text参数更名为universal_newlines

shell ,如果shell设置为true,命令会通过真正的shell环境执行。

# win10 1809 python3.7

import subprocess

subprocess.run('dir', shell=True)

CompletedProcess(args='dir', returncode=0)

popen对象

底层的进程创建和管理使用Popen class,灵活性比较高。

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

Popen.send_signal(signal)

Popen.terminate() 停止

Popen.kill() 杀死子进程

Popen.pid 子进程的进程号

Popen.returncode 此进程的退出码

Popen.poll() 检查子进程是否停止

Popen.wait(timeout=None) 等待子进程停止,如果子进程超时还不termiate,会TimeoutExpired 异常

Popen.communicate(input=None, timeout=None)与进程交互,发送数据到stdin,并读取stdout和stderr直到eof

会返回一个元组(stdout_data, stderr_data),

Popen.stdin

如果 stdin 参数为 PIPE,此属性是一个类似 open() 返回的可写的流对象。如果 encoding 或 errors 参数被指定或者 universal_newlines 参数为 True,则此流是一个文本流,否则是字节流。如果 stdin 参数非 PIPE, 此属性为 None。

Popen.stdout

如果 stdout 参数是 PIPE,此属性是一个类似 open() 返回的可读流。从流中读取子进程提供的输出。如果 encoding 或 errors 参数被指定或者 universal_newlines 参数为 True,此流为文本流,否则为字节流。如果 stdout 参数非 PIPE,此属性为 None。

Popen.stderr

如果 stderr 参数是 PIPE,此属性是一个类似 open() 返回的可读流。从流中读取子进程提供的输出。如果 encoding 或 errors 参数被指定或者 universal_newlines 参数为 True,此流为文本流,否则为字节流。如果 stderr 参数非 PIPE,此属性为 None。

原来python官方文档中文也更新了不少

python3 shell脚本开发_python3 subprogress 模块的使用 代替shell编写脚本相关推荐

  1. JavaScript、Lua语言基础、电脑脚本、手机免ROOT免越狱脚本开发免费视频教程

    JavaScript基础教程:https://www.bilibili.com/video/BV1YW411T7GX Lua基础教程:https://www.bilibili.com/video/BV ...

  2. arcgis python脚本实现从界面选择输入输出_arcgis python脚本实现从界面选择输入输出_ArcGIS Python编程案例(2)-使用ArcPy编写脚本......

    本章将涉及以下案例: 使用ArcGIS Python窗体 访问ArcPy 从脚本中执行工具 使用ArcGIS桌面软件帮助系统 使用变量存储数据 访问ArcPy中的模块 引言 地理处理任务往往是耗时且重 ...

  3. 编写脚本电脑怎么编写界面_在任何无法理解的情况下,请编写脚本

    编写脚本电脑怎么编写界面 脚本编写是使您的应用程序在运行时就可根据客户需求进行调整的最流行的方法之一. 与往常一样,此方法不仅带来好处,例如,在灵活性和可管理性之间存在众所周知的折衷方案. 本文不是从 ...

  4. 在任何无法理解的情况下,请编写脚本

    脚本编写是使您的应用程序在运行时就可以根据客户需求进行调整的最流行的方法之一. 与往常一样,此方法不仅带来好处,例如,在灵活性和可管理性之间存在众所周知的折衷方案. 本文不是从理论上讨论优缺点的文章之 ...

  5. Linux开发 安装JDK8、mysql8.0、shell脚本编写、Ubuntu安装 p4

    前面的笔记: Linux 学习笔记1 安装linux详细教程_linux系统 setting_O丶ne丨柒夜的博客-CSDN博客 Linux 学习笔记2 常用命令_O丶ne丨柒夜的博客-CSDN博客 ...

  6. python3常用模块_Python3 常用模块

    一.time与datetime模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们 ...

  7. 《Linux命令行和Shell脚本开发圣经》:内容目录

    致谢 导语 第一部分:Linux命令行 第一节:从Linux Shell开始 1 Linux是什么? 1.1 管窥Linux内核(Kernel) 1.2 GNU应用程序 1.3 Linux桌面环境 2 ...

  8. Shell脚本开发实战--工具篇

    一.产生背景 企业应用开发中,我们会经常和Linux服务器打交道,比如:Linux提交spark作业,查看作业日志,准备基础数据,调试与自测等:如果每次测试都重复那些操作就显得很业余了,我们想到的肯定 ...

  9. shell脚本学习(一):shell脚本开发的基本规范和习惯

    1.脚本第一行指定脚本解释器 #!/bin/bash 或 #!/bin/sh 2.脚本开头增加作者.脚本作用描述等信息 1 #!/bin/bash 2 #Author: iskylite 3 #Blo ...

  10. 【shell脚本开发技术】linux中shell script脚本开发编程常用命令与脚本速记指南

    shell script #!/bin/bash # do something run shell script sh script.shorchmod a+x script.sh ./script. ...

最新文章

  1. MNE-Python从Raw对象中解析event
  2. 高等数学同济第七版课后答案下册
  3. iPod/iTouch/ipad/iPhone新手注意事项
  4. Starzhou:EOSIO1.0 版本环境搭建
  5. mysql数据库详解(续一)
  6. C语言之rand()和srand()函数
  7. 《SuperMap GIS二次开发MVC实战训练---江海区慢性病防治院系统》项目研发阶段性总结
  8. git常用命令/mac上从零完成本地上传和下载github代码
  9. 传说之下音乐计算机版,传说之下同人音乐
  10. gulp之gulp-uglify模块
  11. Security+ 学习笔记36 嵌入式系统安全
  12. 制作U盘免疫病毒文件夹
  13. 【数据库】解剖式学习无损分解
  14. 关于程序代码的时间复杂度
  15. 主机计算机怎么打开共享,steam怎么开家庭共享方式
  16. 【魔方攻略】斜转魔方教程(原创)
  17. Solidity基础教程2——Safemath
  18. MySQL表中的联合查询
  19. C语言-广度优先遍历
  20. 数据库输出带字段注释的查询

热门文章

  1. L2TP-***通用原理取证及在华为防火墙上的实施
  2. android ssl-dl
  3. RedHat Linux 5企业版安装telnet
  4. [转载] 中华典故故事(孙刚)——33 人上一百形形色色
  5. WinForm中窗体间的数据传递(二)
  6. TextMate里添加Theme
  7. day01 格式化输出和while循环的两个小练习
  8. NA-NP-IE系列实验2
  9. mysql简单命令行操作以及环境变量的配置
  10. [转][整理]Linux下找不到共享库(shared object)