如何在python中自动回答是? [英] How can I automatically answer yes in python?
问题描述
假设我有一个脚本运行 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
我的脚本能够检测到配置文件提示,但我应该如何自动回答是?
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中自动回答是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!