How to manipulate os.system outputs in python?

140 views
Skip to first unread message

chat...@gmail.com

unread,
Nov 21, 2015, 5:31:00 PM11/21/15
to Python GCU Forum
Hi, I am using the 'os.system' in python to run a bash script which returns an xml output, i.e. <ns2:MyVar time="... </ns2:MyVar>

The issue I am having is manipulating the output.

I have output = os.ystem('xmlout')

I then convert a string 

mystroutput = str(output)

However, when I do a print of mystroutput[1], it says:

IndexError: string index out of range

I have 'import os', 'import string' and 'import fileinput' what am I doing wrong?

Robert Mandić

unread,
Nov 22, 2015, 2:42:11 AM11/22/15
to chat...@gmail.com, Python GCU Forum

Heya,

 

os.system returns exit status of the command and not the ouput.

Try os.popen instead:

 

>>> import os

>>> data = os.popen('cat /etc/resolv.conf')

>>> data.read()

--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

 

chat...@gmail.com

unread,
Nov 22, 2015, 4:57:02 PM11/22/15
to Python GCU Forum

I am getting an error when I attempt to print(data).

I did import os
data=os.popen('my bash command')
data.read()

<open file 'my bash command', mode 'r' at 0x7f720435fae0>

I would like to get data into a string so that I can parse it accordingly. 

Should I use

Robert M.

unread,
Nov 23, 2015, 2:33:49 AM11/23/15
to python-g...@googlegroups.com
print data.read()

From: chat...@gmail.com
Sent: ‎11/‎22/‎2015 22:57
To: Python GCU Forum
Subject: Re: How to manipulate os.system outputs in python?

Reply all
Reply to author
Forward
0 new messages