By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Return Text from popen

P: n/a
I know this problem has a very simple answer, but I've checked the web and I
don't understand the answers that I've found.

How do I return text from a standard Linux command?

For example: I want to read the stdout results of a typical linux command
(such as "df") into a Python variable.

I've tried these techniques:

result = os.system("df")
It dumps the stdout to a screen and puts the result code into "result"

result = popen2("df")
It gives me a tuple of the stdin & stdout pipes, which I don't know how to
harvest into the actual stdout result.
Can someone give me a brief code example of how to get the actual text
result from the linux command into a Python variable? Thanks!

--
Casey Bralla
Chief Nerd in Residence
The NerdWorld Organisation
http://www.NerdWorld.org
Oct 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Oct 15, Casey Bralla wrote:
How do I return text from a standard Linux command?

For example: I want to read the stdout results of a typical linux
command (such as "df") into a Python variable.

from os import popen
p = popen("df")
p <open file 'df', mode 'r' at 0xf6f685e0> df_out = p.read()
p.close()
p <closed file 'df', mode 'r' at 0xf6f685e0> print df_out


--
Micah Elliott
<md*@micah.elliott.name>
Oct 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.