在某些shell脚本中,您需要确认“yes”来运行shell,一个更简单的方法是使用“yes”和pipe,就像这样:如何在python中使用os.system()来运行shell命令
yes | test.py
然后,您可以自动运行shell脚本而不会回答“是”了。 今天,当我通过尝试使用python时:os.system(“yes | **。sh”),我得到了一个错误。
这里是我的test.py文件:
import os
def f():
cmd1 = "yes | read "
os.system(cmd1)
f()
,并在外壳键入如下命令运行:蟒蛇test.py.故障信息是: 是:标准输出:断开的管道 是:写入错误
但是如果我在shell中输入“yes | read”,它工作正常。 有谁可以告诉我为什么?
不要;使用'subprocess'模块。 – geoffspear