如何在python中使用os.system()运行shell命令 [英] how to use os.system() in python for running an shell order
问题描述
在某些shell脚本中,您需要确认是"才能运行外壳程序,嗯,一种更简单的方法是使用是"和管道,如下所示:
In some shell script, you need to confirm "yes" to run the shell, well, an easier way is using "yes" and pipe, like this:
yes | test.py
然后,您可以自动运行shell脚本,而无需再回答是". 今天,当我尝试通过os.system("yes | **.sh")在python中使用它时,出现了故障.
then, you can run the shell script automatically without answer "yes" anymore. today, when i use this in python by trying : os.system("yes|**.sh"), i got an fault.
这是我的test.py文件:
Here is my test.py file:
import os
def f():
cmd1 = "yes | read "
os.system(cmd1)
f()
并通过键入以下命令在shell中运行:pythontest.py.故障信息为: 是:标准输出:断管 是的:写错误
and run in shell by typing : python test.py. the fault information is : yes: standard output: Broken pipe yes: write error
但是,如果我在shell中键入"yes | read",则效果很好. 谁能告诉我为什么?
but if i type "yes|read" in shell,it works well. may anyone tell me why?
推荐答案
尝试一下
import os
def f():
cmd1 = "echo 'yes' | read "
os.system(cmd1)
f()
这篇关于如何在python中使用os.system()运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!