howtechstuffworks
howtechstuffworks

Reputation: 1926

Need to get output as lists rather than strings in Popen or any other system commands

I am trying to run a command from Python script(using Popen()) get the output as list, instead of string.

For Example, When I use Popen(), it gives the output as string. For commands like "vgs, vgdisplay, pvs, pvdisplay", I need to get the output as lists and should be able to parse it row and column, so that I can do the necessary action(like deleting the already existing Vg's etc etc). I was just wondering, if it is possible to get as lists or atleast convert into lists....

I started learning python a week ago, so I might have missed some simple tricks, please pardon me.....

Upvotes: 2

Views: 7020

Answers (1)

Jakob Bowyer
Jakob Bowyer

Reputation: 34698

Just to elaborate on the existing comments

from subprocess import PIPE
import subprocess
pro = subprocess.Popen("ifconfig", stdout=PIPE, stderr=PIPE)
data = pro.communicate()[0].split()
for line in data:
    print "THIS IS A LINE"
    print line
    print "**************"

Upvotes: 5

Related Questions