如何使用python apt模块删除ppa?

2024-06-02 13:01:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以添加一个ppa使用它,但不能删除。我找不到从中删除ppa的正确语法源.list. 这是我的密码:

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)的格式


Tags: importhttp密码netlaunchpadas语法repo
2条回答

^{}帮助文本读取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()

我现在用这个做移除。你知道吗

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()

相关问题 更多 >