Python3中的subprocess.check_output函数可以执行一条sh命令,并返回命令的输出内容,用法如下:

output = subprocess.check_output(["python3", "xx.py"], shell = False)

该函数两个参数第一个表示命令内容,因为中间有空格所以用中括号这种形式,同时制定shell=False表示命令分开写了。而该命令执行后的输出内容会返回给output变量。

需要注意的是这个output变量并不是一个string,也就是说不能用string的一些函数,比如你想知道返回的输出中是否包含某个字符串:

output = subprocess.check_output(["python3", "xx.py"], shell = False)
if (output.find("yes") >= 0): print("yes")
else: print("no")

这样执行后不会有任何输出,因为find()函数是给string用的,而这里的output其实不是一个string,那它是个什么呢?

我们看看python3的subprocess.check_output的文档:

By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.

也就是说,返回的其实是一个编码后的比特值,实际的编码格式取决于调用的命令,因此python3将解码过程交给应用层,也就是我们使用的人来做。

这样就清晰了,要对输出使用stirng的操作,需要先通过解码将其转换成string:

output = subprocess.check_output(["python3", "xx.py"], shell = False)
out = output.decode()
if (out.find("yes") >= 0): print("yes")
else: print("no")

这样就可以正常判断了。

python3 subprocess.check_output 执行shell命令 返回结果相关推荐

  1. Python subprocess.check_output 执行shell命令 返回结果(单次执行shell命令)

    Python3中的subprocess.check_output函数可以执行一条sh命令,并返回命令的输出内容,用法如下: output = subprocess.check_output([&quo ...

  2. Centos执行shell命令返回127错误

    Centos执行shell命令返回127错误 在终端可以正常运行 web执行shell 返回127错误 解决方法 应该是运行环境和登录用户的运行环境存在差异造成的 在脚本执行之前,添加. /etc/p ...

  3. python的subprocess模块执行shell命令

    subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...

  4. java shell 127_java执行shell命令返回127 :No such file or directory

    场景:需要在指定的远端机器执行某些shell命令 过程: java中执行以下代码: Process process = Runtime.getRuntime().exec(realCommond); ...

  5. pythonsubprocess执行多条shell命令_python中subprocess批量执行linux命令

    可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 popen --废弃 commands --废弃,3.x中被移除 以上执行shell命令的 ...

  6. python commands模块_python之commands和subprocess入门介绍(可执行shell命令的模块)

    一.commands模块 1.介绍 当我们使用Python进行编码的时候,但是又想运行一些shell命令,去创建文件夹.移动文件等等操作时,我们可以使用一些Python库去执行shell命令. com ...

  7. linux c 执行shell命令并获取返回结果

    最近在项目中用到了C语言执行shell命令的问题,调查了一下,一般有system函数.exec族函数,但是还不太清楚怎么获取shell命令执行的返回信息. 例如执行一个ifconfig命令,肯定需要获 ...

  8. Python 执行 shell命令 的几个方法小结

    参考:https://blog.csdn.net/ronnyjiang/article/details/53333538 Python 执行 shell 命令 Python 可以执行 shell 命令 ...

  9. python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?

    原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...

最新文章

  1. Service Manger的初始化分析
  2. flutter怎么手动刷新_flutter局部刷新的实现示例
  3. js判断是否为数字_第23题:JavaScript 中如何判断变量是否为数字 ?
  4. IOS基础之愤怒的小方块
  5. php 正则特殊字符转义,php 正则特殊字符转义的方法
  6. 跨地域的VPC私网互通【高速通道案例】
  7. linux批量替换文件名中的相同字符
  8. c语言数组指定位置插入和删除_玩转C语言链表,单链表/双向链表的建立/遍历/插入/删除...
  9. roboware下载链接
  10. onselect 与 onselectstart
  11. Django中间件之csrf跨站请求伪造
  12. [2019.7.31~2019.8.15]纪中集训游记
  13. 常见的html转义字符
  14. 为什么公有云在中国落地难
  15. 数字电路13-任意进制计数器设计
  16. centos查询 硬盘序列号查询_CentOS 查看硬件信息
  17. el-table合并单元格附表单包含表格
  18. 视频教程-JavaScript实战讲解课程-Java
  19. linux挂载设备目录命令,使用mount命令进行设备或者文件系统挂载时,需要用到的设备名称位于( )目录...
  20. 浅谈特征方程及相关证明

热门文章

  1. Linux 操作系统原理 — 零拷贝技术
  2. python使用笔记:xlrd和xlwt库的使用
  3. iOS 利用RunTime检测控制器是否销毁
  4. 基于DDD的.NET开发框架 - ABP领域服务
  5. [转载]项目风险管理七种武器-多情环
  6. L4 如何在XCode中下进行工作
  7. JSON调试找不到 net.sf.ezmorph.Morpher问题解决
  8. Windows 8的圣战
  9. 如何将json格式的string字符串转换为string数组
  10. 纪念:2006年我在51CTO的第一帖