0

I am using Fedora 17 xfce and I am programming in Python 2.7.3. Fedora uses a package manager called yum. I have a python script that searches for packages like this:

import os
package = raw_input("Enter package name to search: ")
os.system("yum list " + package)

So I want python to check if in the output of this command exists the words No matching packages to list. I checked a similar question and I tried some methods here but the string contained only the first line of the output.

Thanks in advance

Community
  • 1
  • 1

2 Answers2

4

os.system will not return any of the output. The question you linked to has the right answer. If you only got the first line of the output, maybe you were trying to read it line by line?

The right way to get the entire output is this:

import subprocess
package = raw_input("...")
p = subprocess.Popen(["yum", "install", package], stdout=subprocess.PIPE)
out, err = p.communicate()
# Wait for the process to exit before reading
p.wait()    

full_output = out.read()
jjm
  • 6,028
  • 2
  • 24
  • 27
2

You would want to use the subprocess module for that, since os.system() simply returns the exit code of a command:

from subprocess import check_output
out = check_output(['yum', 'list', raw_input('package name')])

You could also use Yum's API directly to search packages:

from yum import YumBase

base = YumBase()
for package, name in base.searchGenerator(['name'], ['python']):
    print(package.name, package.version)
gvalkov
  • 3,977
  • 30
  • 31