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

我正在编写一个为用户安装命令行界面的脚本。


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 string

output = 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-runtime

Suggested packages:

  ctags vim-doc vim-scripts

The following NEW packages will be installed:

  vim vim-runtime

0 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(),但没有进行多次输入。


千巷猫影
浏览 133回答 1
1回答

qq_遁去的一_1

-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')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python