Save the output of a command in a string in linux using python

后端 未结 2 1502
时光取名叫无心
时光取名叫无心 2021-01-29 06:50

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:

         


        
2条回答
  •  遇见更好的自我
    2021-01-29 07:31

    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)
    

提交回复
热议问题