当在python linux中执行命令os.system()时,在python中给出响应yes/no [英] Give response yes/no in python when a command is executed os.system() in python linux

查看:774
本文介绍了当在python linux中执行命令os.system()时,在python中给出响应yes/no的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑类似

yum install boto

在终端中执行时,要问是/否

When I execute in terminal, to proceed is asks me for yes/no

我可以像这样用python回应吗

Can I respond to it in python like

os.system("yum install boto")

下一个"Yes"将通过相同的python代码传递到终端,以便安装.好吧,我认为这行不通.如果是在上述声明之后写的

Next "Yes" is to be passed to terminal through the same python code so that it installs. Well, I dont think this works. If it is written after tha above statement

os.system("yes")

请告诉我是否可行?

推荐答案

您可以使用subprocess.Popen并将其写入stdin,需要sudo的-S标志,然后是其余命令.

You can use subprocess.Popen and write to stdin, you need the -S flag for sudo then just the rest of the commands.

from subprocess import Popen, PIPE
import getpass

pwd = getpass.getpass()
proc = Popen(['sudo', '-S', rest of commands ],stdout=PIPE, stdin=PIPE, stderr=PIPE,universal_newlines=True)
proc.stdin.write("{}\n".format(pwd))
out,err = proc.communicate(input="{}\n".format("yes"))

这篇关于当在python linux中执行命令os.system()时,在python中给出响应yes/no的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆