如何在 Python 3 中使用 stdin 将字符串传递给 subprocess.run [英] How do I pass a string in to subprocess.run using stdin in Python 3

查看:28
本文介绍了如何在 Python 3 中使用 stdin 将字符串传递给 subprocess.run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这肯定是在其他地方关于子流程的长期讨论的一部分.但答案很简单,应该打破.

This is certainly answered as part of a long discussion about subprocess elsewhere. But the answer is so simple it should be broken out.

如果我使用 Python 3 的 subprocess.run(),如何将字符串foo"传递给在 stdin 上期望它的程序?

How do I pass a string "foo" to a program expecting it on stdin if I use Python 3's subprocess.run()?

推荐答案

最简单的例子,将 foo 发送到 cat 并让它打印到屏幕上.

Simplest possible example, send foo to cat and let it print to the screen.

 import subprocess

 subprocess.run(['cat'],input=b'foo\n')

注意你发送的是二进制数据和回车.

Notice that you send binary data and the carriage return.

这篇关于如何在 Python 3 中使用 stdin 将字符串传递给 subprocess.run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆