为了账号安全,请及时绑定邮箱和手机立即绑定

如何向 Popen() 传递多个输入参数?

如何向 Popen() 传递多个输入参数?

千巷猫影 2023-06-27 17:24:04
我正在编写一个为用户安装命令行界面的脚本。proc = Popen("sudo -S apt-get install vim".split(), stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)# Popen only accepts byte-arrays so you must encode the stringoutput = proc.communicate(password.encode())stdoutput = (output)[0].decode('utf-8')输出:Installing Vim...|Reading package lists...Building dependency tree...Reading state information...The following additional packages will be installed:  vim-runtimeSuggested packages:  ctags vim-doc vim-scriptsThe following NEW packages will be installed:  vim vim-runtime0 upgraded, 2 newly installed, 0 to remove and 5 not upgraded.Need to get 7,111 kB of archives.After this operation, 34.6 MB of additional disk space will be used.Do you want to continue? [Y/n] Abort.如您所见,软件包的安装停止Do you want to continue?,并且由于某种原因,安装被中止。我如何y在这个阶段使用传递 a Popen()?我已经使用传递密码communicate(),但没有进行多次输入。
查看完整描述

1 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

-y只需作为选项传递apt-get即可避免与它(进一步)交互的需要。


proc = Popen(["sudo", "-S", "apt-get", "-y", "install", "vim"], stdin=PIPE, stdout=PIPE, stderr=PIPE)

output, error = proc.communicate(password.encode())

stdoutput = output.decode('utf-8')


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信