如何获取可用升级包的列表并使用 python 将其写入文件?
当我运行apt-get upgrade > output
它时,它可以在 bash 中运行。我想我必须在 python 程序中发送陷阱信号( Ctrl+ )。C
关于如何实现这一目标的任何建议?
我现在在代码上尝试了这个:
#!/usr/bin/env python
import subprocess
apt = subprocess.Popen([r"apt-get", "-V", "upgrade", ">", "/usr/src/python/upgrade.log"], stdin=subprocess.PIPE)
apt_stdin = apt.communicate()[0]
但它退出并且不写入文件。
这可行,但是当我将其移植到其他 Debian 系统时出现错误:
import apt
cache=apt.Cache()
cache.update()
cache.open(None)
cache.upgrade()
for pkg in cache.get_changes():
# print pkg.name, pkg.summary
fileHandle = open('/tmp/upgrade.log', 'a')
fileHandle.write(pkg.name + " - " + pkg.summary + "\n")
和错误....
/usr/lib/python2.5/site-packages/apt/__init__.py:18: FutureWarning: apt API not stable yet
warnings.warn("apt API not stable yet", FutureWarning)
Traceback (most recent call last):
File "apt-notify.py", line 13, in <module>
for pkg in cache.get_changes():
AttributeError: 'Cache' object has no attribute 'get_changes'