为了澄清一些问题:

由于jro有mentioned,正确的方法是使用subprocess.communicate。

然而,当使用带有input的subprocess.communicate来馈送stdin时,需要根据docs来启动带有stdin=subprocess.PIPE的子进程。Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

还有qed在注释中提到,对于Python 3.4,您需要对字符串进行编码,这意味着您需要将字节传递给input,而不是string。这并不完全正确。根据文档,如果流是以文本模式打开的,则输入应该是字符串(源是同一页)。If streams were opened in text mode, input must be a string. Otherwise, it must be bytes.

因此,如果流不是在文本模式下显式打开的,则应该执行以下操作:import subprocess

command = ['myapp', '--arg1', 'value_for_arg1']

p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

output = p.communicate(input='some data'.encode())[0]

我特意把上面的stderr值留作STDOUT示例。

也就是说,有时您可能需要另一个进程的输出,而不是从头开始构建它。假设您希望运行与echo -n 'CATCH\nme' | grep -i catch | wc -m等价的代码。这通常会返回“CATCH”中的数字字符和换行符,结果是6。回声的作用是将CATCH\nme数据馈送给grep。因此,我们可以用Python子进程链中的stdin作为变量将数据馈送给grep,然后将stdout作为管道传递给wc进程的stdin(同时,去掉多余的换行符):import subprocess

what_to_catch = 'catch'

what_to_feed = 'CATCH\nme'

# We create the first subprocess, note that we need stdin=PIPE and stdout=PIPE

p1 = subprocess.Popen(['grep', '-i', what_to_catch], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

# We immediately run the first subprocess and get the result

# Note that we encode the data, otherwise we'd get a TypeError

p1_out = p1.communicate(input=what_to_feed.encode())[0]

# Well the result includes an '\n' at the end,

# if we want to get rid of it in a VERY hacky way

p1_out = p1_out.decode().strip().encode()

# We create the second subprocess, note that we need stdin=PIPE

p2 = subprocess.Popen(['wc', '-m'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

# We run the second subprocess feeding it with the first subprocess' output.

# We decode the output to convert to a string

# We still have a '\n', so we strip that out

output = p2.communicate(input=p1_out)[0].decode().strip()

这与响应here有些不同,在响应中,您可以直接对两个进程进行管道传输,而无需在Python中直接添加数据。

希望能帮上忙。

python stdin.write_如何写入Python子进程的stdin?相关推荐

  1. python读取txt文件写入-Python读写txt文本文件的操作方法全解析

    一.文件的打开和创建 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python! hello world! ...

  2. python 文件指定位置写入-Python从文件中读取指定的行以及在文件指定位置写入...

    Python从文件中读取指定的行 如果想根据给出的行号, 从文本文件中读取一行数据, Python标准库linecache模块非常适合这个任务: 测试文件内容 :This is line 1. Thi ...

  3. python读取txt文件写入-python 读取、写入txt文件的示例

    写入文件 使用open()函数和write()函数 但是有两种写法,分别是'a'和'w' 'a' 表示写入文件 若无该文件会直接创建一个 如果存在这个文件,会接着已有的内容的后面写入 with ope ...

  4. python读取txt文件写入-python txt文件的写入和读取

    1.文件的打开 使用open () 函数 打开文件.他有两个参数,文件路径或文件名和文件的打开方式. "r" 只读模式,不能编辑和删除文件内容. "w" 写入模 ...

  5. python创建文件并写入-python新建txt文件,并逐行写入数据

    Element is not currently visible and so may not be interacted with错误 用selenium定位时,碰到这种错误的原因: 此种问题的关键 ...

  6. python创建_python 创建txt并写入Python基础1 Hello World!

    从今天开始和大家分享一下python最基础的知识,以便帮助初学者快速入门. 最最基础的当然是hello world 了,无论哪门语言都会从它开始... 简单的'Hello World!' 1. 直接运 ...

  7. python stdin和stdout_无法使用Python写入和读取stdin / stdout

    我在MacOS 10.7.4上使用Python 2.7.这是一个较长的脚本(不是我写的)的一部分,它基本上为PHP提供了一些配置选项,将它们写入PHP的stdin,然后从它的stdout中读取它们. ...

  8. Python 运行 Python hello.py 出错,提示: File stdin , line 1

    写了一个hello.py,仅有一句,print 'hello world', 运行 Python hello.py 出错,提示: File "<stdin>" , li ...

  9. python输出csv文件-Python之读取与写入CSV文件

    原标题:Python之读取与写入CSV文件 本文作者:闫续文 文字编辑:张梦婷 技术总编:张学人 有问题,不要怕!访问 CSV(Comma-Separated Values)格式是电子表格和数据库最常 ...

  10. python输出文本-python;如何将输出写入文本文件

    用我的代码,我遍历文件并计算文件中的模式.我的代码如下python;如何将输出写入文本文件 from collections import defaultdict import csv, os, re ...

最新文章

  1. ViewPager 的点击事件回调
  2. Matlab怎么计算信号的能量,用Matlab求离散讯号的能量与功率怎么编程
  3. WebStorm React JS语法报错问题
  4. 第一阶段:Java基础之控制结构
  5. FFmpegh.264解码
  6. CS231n Convolutional Neural Networks for Visual Recognition------Numpy Tutorial
  7. vue锚点定位(代码通用) - 总结篇
  8. jq双击放大图片_痘痘肌肤反馈图片,平时注意这3个就可以
  9. 深入剖析ORACLE数据库备份与恢复的原理
  10. 创科视觉软件说明书_【拓斯达 | GGII】20192023年中国机器视觉行业调研
  11. 网游中的网络编程系列1:UDP vs. TCP
  12. iOS-----------关于UDID
  13. BI_DBA_安装(3):安装informatic
  14. android输入法剪贴板,手机写作利器:输入法剪贴板
  15. python 语音处理工具包AudioSegment的基本使用
  16. 韵达快递 | 快递单号查询API
  17. elastic APM 深入测试 一 (无嵌套调用的分布式微服务监控)
  18. 2023中国科学院大学计算机考研信息汇总
  19. c语言打印日历的程序,简单日历打印(C语言)
  20. OOM以及垃圾收集器

热门文章

  1. Python,还有一些鲜为人知的特性!你知道吗?
  2. ping的通百度但是浏览器打不开的解决方法
  3. CSS(层叠样式表(Cascading Style Sheets))历史
  4. A - Browsing History
  5. 31岁,追忆似水流年。。。
  6. 基于Android点菜系统的设计与实现,基于Android的手机点菜系统的设计与实现论文.doc...
  7. Unity学习笔记:观察者模式
  8. 基于卫星测深的牙买加沿岸水深测量
  9. Idear创建Maven项目
  10. aardio设置透明窗体说明