0

如何使用 Python/python-apt 以编程方式获得与“dpkg --get-selections”相同的结果?

4

1 回答 1

2

要显示前五个包:

import apt_pkg

apt_pkg.init()
cache = apt_pkg.Cache()

i = 5
for pkg in cache.Packages:
  print(pkg)
  i -= 1
  if i < 0:
    break

输出(在我的 Ubuntu 系统上运行时):

$ python apt_package_list.py
Reading package lists... Done
Building dependency tree       
Reading state information... Done
<apt_pkg.Package object: name:'tesseract-ocr-epo' section: 'universe/graphics' id:48834>
<apt_pkg.Package object: name:'omninotify' section: '' id:48674>
<apt_pkg.Package object: name:'pipenightdreams' section: 'universe/games' id:43500>
<apt_pkg.Package object: name:'mumudvb' section: 'universe/misc' id:41568>
<apt_pkg.Package object: name:'mpg123-alsa' section: '' id:41448>
<apt_pkg.Package object: name:'tbb-examples' section: 'universe/doc' id:38301>
于 2013-07-09T06:27:45.887 回答