Kokos
Kokos

Reputation: 13

How to store multiple stdout results in a variable?

These are my first steps in Python 3.4 and also I haven't been programming anything for a longer time..

I have a Linux operating system command which returns many but one result per line on stdout.

What would be the best option to capture these results for further processing? I think that ideally would be to store every result line in separate table entries. But maybe you have some better ideas.

Upvotes: 0

Views: 120

Answers (2)

glglgl
glglgl

Reputation: 91017

If you call the command via

sp = subprocess.Popen(['command', 'arg1', 'arg2'], stdout=subprocess.PIPE)

you can do

for line in sp.stdout:
    do_stuff_with(line)

Upvotes: 2

Chris Arena
Chris Arena

Reputation: 1620

Direct the stdout to a file:

'some linux command' > output.txt

Then with python it's simple to open and manipulate:

with open('output.txt') as my_file:
    content = my_file.readlines()

You'd have to give more details about your command and output for me to give a more specific suggestion.

Upvotes: 0

Related Questions