如何在python中自动回答yes? [英] How can I automatically answer yes in python?

查看:342
本文介绍了如何在python中自动回答yes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个稍后会运行apt upgrade -y的脚本,我将需要针对不同的软件包手动回答这些问题.我如何自动做到这一点?

Say I have a script that runs apt upgrade -y later on I'll need to manually answer these questions for different packages. How can I automatically do that?

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

Setting up 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

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

My script is able to detect the confile prompt but how should I go about answering yes automatically?

通过使用python-apt api,我可以通过此功能检测到提示

by using python-apt api I am able to detect the prompt via this funtion

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

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

推荐答案

对于所有像我这样迷路的人,答案是:

For all those who were lost like me, the answer is:

import apt_pkg

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

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

This will make apt automatically accept new config files.

这篇关于如何在python中自动回答yes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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