如何在python中自动回答yes?

时间:2017-04-10 07:11:21

标签: python debian apt

假设我之后运行apt upgrade -y的脚本我将需要为不同的软件包手动回答这些问题。我该如何自动执行此操作?

设置virtualbox-guest-x11(5.0.32-dfsg-0ubuntu1.16.04.2)...

Configuration file '/etc/X11/Xsession.d/98vboxadd-xclient'
 ==> File on system created by you or by a script.
 ==> File also in package provided by package maintainer.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** 98vboxadd-xclient (Y/I/N/O/D/Z) [default=N] ?  conffile prompt detected: /etc/X11/Xsession.d/98vboxadd-xclient /etc/X11/Xsession.d/98vboxadd-xclient.dpkg-new

我的脚本能够检测到容易提示,但我该如何自动回答?

通过使用python-apt api我能够通过此功能检测提示

def conffile(self, current, new):
    print " conffile prompt detected: %s %s" % (current, new)

    """(Abstract) Called when a conffile question from dpkg is detected."""

3 个答案:

答案 0 :(得分:1)

由于(根据问题发表评论)您正在使用Python-APT,因此首先想到的是使用Configuration类设置conf['APT::Get::Assume-Yes'] = True

答案 1 :(得分:1)

使用yes命令:

yes Y | apt ...

答案 2 :(得分:0)

对于像我一样迷失的所有人,答案是:

import apt_pkg

apt_pkg.init_config()
apt_pkg.config.set("DPkg::Options::", "--force-confnew")

这将使apt自动接受新的配置文件。