Give response yes/no in python when a command is executed os.system() in python linux

▼魔方 西西 提交于 2019-12-04 20:08:48

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"))

You can add a pipe and do

yes | os.system("yum install boto")

it will repeat yes until the command is done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!