使用Popen方法时,需要获取输出内容时可以按如下方法获取:

 # -*- coding:utf-8 -*-

 import subprocess
cmd = r"ping www.baidu.com"
result = subprocess.Popen(cmd, stdout=subprocess.PIPE) # 将输出内容存至缓存中
print(result.stdout.read().decode("gbk")) # 通过从缓存中读取内容并解码显示 输出显示如下:
正在 Ping www.wshifen.com [103.235.46.39] 具有 32 字节的数据:
来自 103.235.46.39 的回复: 字节=32 时间=334ms TTL=39
来自 103.235.46.39 的回复: 字节=32 时间=340ms TTL=39
来自 103.235.46.39 的回复: 字节=32 时间=317ms TTL=39
来自 103.235.46.39 的回复: 字节=32 时间=342ms TTL=39 103.235.46.39 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 317ms,最长 = 342ms,平均 = 333ms Process finished with exit code 0

获取Popen的输出时,可以通过 stdout从缓存中读出来,那怎么写到缓存中呢,只需要在Popen方法的参数中带上stdout=subprocess.PIPE这个关键字参数即会写入到缓存中,当然了,这个里面还有一个参数stdin这个关键字参数,这个参数可以接收到从其它管道中的输出做为这次的输入,例如:

  import subprocess
child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)
child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE)
out = child2.communicate()

subprocess 的 Popen用法的更多相关文章

  1. Python多进程(1)——subprocess与Popen()

    Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...

  2. Python执行系统命令:使用subprocess的Popen函数

    使用subprocess的Popen函数执行系统命令 参考: http://blog.sina.com.cn/s/blog_8f01450601017dlr.html http://blog.csdn ...

  3. Python用subprocess的Popen来调用系统命令

    当我们须要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.可是这两个命令过于简单,不能完毕一些复杂的操作,如给执行的命令提供输入或者读取命令的输出, ...

  4. Python使用subprocess的Popen要调用系统命令

    当我们须要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.可是这两个命令过于简单.不能完毕一些复杂的操作,如给执行的命令提供输入或者读取命令的输出, ...

  5. 【Android自动化】Subprocess.check_output()简单用法

    # -*- coding:utf-8 -*- import os import sys import subprocess from uiautomator import device as d cm ...

  6. subprocess之check_out用法

    在python3中使用subprocess的check_out方法时,因为该输出为byte类型,所以如果要查看具体的内容时需要进行转码,如果转码不对话,会影响内容输出的可读性,如下: #1,输出解码不 ...

  7. python模块struct和subprocess

    准确地讲,Python没有专门处理字节的数据类型.但由于str既是字符串,又可以表示字节,所以,字节数组=str.而在C语言中,我们可以很方便地用struct.union来处理字节,以及字节和int, ...

  8. 模块sys, os, glob, pickle, subprocess常见用法

    参考python常用标准库 http://blog.51cto.com/lizhenliang/1872538 一. sys   1. sys.argv 脚本名1.py, 命令行中执行python 1 ...

  9. python中的popen和subprocess

    import os from subprocess import Popen, PIPE res = os.popen('xx.exe E:\\test\\file1 E:\\test\\file2' ...

随机推荐

  1. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  2. 分布式监控告警平台Centreon快速使用

    一. Centreon概述 Centreon是一款功能强大的分布式IT监控系统,它通过第三方组件可以实现对网络.操作系统和应用程序的监控:首先,它是开源的,我们可以免费使用它:其次,它的底层采用nag ...

  3. Orleans 序列化遇到的坑

    真的是巨坑 搞明白问题的我简直无法用言语来描述我的心情 先上架构图 理想中的架构 服务随便上 网关只负责分发 然后跟随官方教程写遇到了序列化问题 以前有经验,不慌,以前稀里糊涂就搞定了. 再然后遇到一 ...

  4. 国内下载vscode速度慢解决

    找到对应的文件,点击下载,会出现一个类似下面的链接: https://az764295.vo.msecnd.net/stable/f06011ac164ae4dc8e753a3fe7f9549844d ...

  5. 【React】383- React Fiber:深入理解 React reconciliation 算法

    作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...

  6. layui扩展组件,下拉树多选

      项目介绍 项目中需要用到下拉树多选功能,找到两个相关组件moretop-layui-select-ext和wujiawei0926-treeselect,但是moretop-layui-selec ...

  7. 【Git】安装配置

    [Git]安装配置 转载:https://www.cnblogs.com/yangchongxing/p/10173231.html 1.在 Ubuntu 上安装 $ sudo apt-get ins ...

  8. 5个点彻底搞清楚SpringBoot注解

    作者:张伯毅 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfig ...

  9. poj 3279 Fliptile (简单搜索)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Descrip ...

  10. springboot 集成jsp

    建立好springboot项目,确定能成功运行 在application.properties文件中添加 server.context-path=/bootserver.port=8080spring ...