【问题标题】:How can I remove a ppa using python apt module?如何使用 python apt 模块删除 ppa?
【发布时间】:2017-04-20 03:51:59
【问题描述】:

我可以使用它添加 ppa,但无法删除。我找不到从sources.list 中删除ppa 的正确语法。这是我的代码:

import aptsources.sourceslist as s
repo = ('deb', 'http://ppa.launchpad.net/danielrichter2007/grub-customizer/ubuntu', 'xenial', ['main'])
sources = s.SourcesList()
sources.add(repo)
sources.save()

#doesn't work 
sources.remove(repo)

我尝试阅读找到here 的文档,但我仍然找不到调用sources.remove(repo) 的格式

【问题讨论】:

标签: python ubuntu apt


【解决方案1】:

SourcesList.remove() 帮助文本为remove(source_entry),这表明它想要的是一个SourceEntry 对象。当它发生时,sources.add() 返回一个 SourceEntry 对象:

import aptsources.sourceslist as sl

sources = sl.SourcesList()
entry = sources.add('deb', 'mirror://mirrors.ubuntu.com/mirrors.txt', 'xenial', ['main'])
print(type(entry))

输出:

<class 'aptsources.sourceslist.SourceEntry'>

要删除条目:

sources.remove(entry)
sources.save()

您也可以禁用它(这将在sources.list 中留下一个注释掉的条目:

entry.set_enabled(False)
sources.save()

【讨论】:

  • 感谢您的解释。
【解决方案2】:

我现在正在使用它来进行删除。

import fileinput 

filename = '/etc/apt/sources.list'
word = 'grub-customizer'
n = ""
remove = fileinput.input(filename, inplace=1)
for line in remove:
    if word in line:
       line = n
    line.strip()
    print line,
remove.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2017-06-04
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多