如何使用 Python 中的 invoke.run() 将字符串传递给可执行文件的 stdin
在我们之前的示例 如何使用 Python 中的 invoke.run() 捕获 stdout 到字符串 中,我们展示了如何使用 invoke.run(..., out_stream=...) 将 stdout 捕获到 StringIO,然后可以转换为字符串。
类似地,我们可以通过首先将字符串转换为 StringIO,然后使用 invoke.run(..., in_stream=...) 将字符串传递给子进程的 stdin。
invoke_pass_stdin_example.py
stdin_str = "abc123" # what we want to pass to stdint
stdin_io = StringIO(stdin_str)
result = invoke.run("myexecutable", in_stream=stdin_in)
# ...Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow