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

后端 未结 2 1451
时光取名叫无心
时光取名叫无心 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:38

    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()
    

提交回复
热议问题