How To Get List Installed Linux Rpms With Python?
I use subprocess.getoutput('rpm -qa').split('\n'),it's not very well. rpmfile module can only read .rpm files Can you help me find one module?
Solution 1:
If you are using Fedora, there is a module called rpm
from the package rpm-python
that will allow you to query the rpm database:
import rpm
ts = rpm.TransactionSet()
mi = ts.dbMatch()
for h in mi:
print"%s-%s-%s" % (h['name'], h['version'], h['release'])
That is a simple piece of code from the documentation. See here for more information.
Solution 2:
Maybe code below is useful for someone.
import os
f = os.popen('rpm -qa')
arq = f.readlines()
#print("First file=" + arq[0].strip())
for x in arq:
print(x)
Solution 3:
I modified code similar to what is posted by Marcus Poli. This was tested using Python 2.7 and 3.6 on CentOS 7.4. My original question was How do I check if an rpm package is installed using Python?
import os
rpm = 'binutils'
f = os.popen('rpm -qa')
arq = f.readlines()
for r in arq:
if rpm in r:
print("{} is installed".format(r.rstrip()))
Output:
binutils-devel-2.27-34.base.el7.x86_64 is installed
binutils-2.27-34.base.el7.x86_64 is installed
Post a Comment for "How To Get List Installed Linux Rpms With Python?"