在某些shell脚本中,需要确认“ yes”才能运行shell,好了,一种更简单的方法是使用“ yes”和管道,如下所示:

yes | test.py


然后,您可以自动运行shell脚本,而无需再回答“是”。
今天,当我尝试通过os.system(“ yes | **。sh”)在python中使用它时,出现了故障。

这是我的test.py文件:

import os
def f():
    cmd1 = "yes | read "
    os.system(cmd1)
f()


然后输入以下命令在shell中运行:pythontest.py。故障信息为:
是:标准输出:断管
是:写入错误

但是,如果我在shell中键入“ yes | read”,则效果很好。
谁能告诉我为什么?

最佳答案

尝试这个

import os
def f():
    cmd1 = "echo 'yes' | read "
    os.system(cmd1)
f()

10-07 13:09
查看更多