【发布时间】:2024-05-29 17:45:02
【问题描述】:
在某些 shell 脚本中,您需要确认“yes”才能运行 shell,嗯,更简单的方法是使用“yes”和管道,如下所示:
yes | test.py
然后,您可以自动运行 shell 脚本而无需再回答“是”。 今天,当我通过尝试在 python 中使用它时:os.system("yes|**.sh"),我遇到了一个错误。
这是我的 test.py 文件:
import os
def f():
cmd1 = "yes | read "
os.system(cmd1)
f()
并通过键入:python test.py 在 shell 中运行。故障信息为: 是:标准输出:断管 是:写入错误
但是如果我在 shell 中输入“yes|read”,它就可以正常工作。 谁能告诉我为什么?
【问题讨论】:
-
不要;使用
subprocess模块。
标签: python shell pipe os.system