473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Return Text from popen

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
1 1963
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tung Wai Yip | last post by:
I've build the following utility. It works so far but I want to make sure I'm doing the right thing. I am running Python 2.3 on windows 2000. def execute(cmd): """ execute cmd in sub-process,...
2
by: Stuart McGraw | last post by:
When I run (Python 2.3.3) this script on a MS Windows 2000 machine: import os print "1st popen..." f = os.popen ("echo " + u"\u0054\u0045\u0053\u0054.txt") print f.read () print "2nd popen..."...
4
by: Yann.K | last post by:
Hello. Using Tkinter, i would create a widget which display a shell command return. This return is long, and i would display a real time display (like with the tail -f commande on Linux) I...
8
by: billiejoex | last post by:
Hi all. I'm searching for a portable (working on *nix and win32) function that executes a system command and encapsulate its output into a string. Searching for the web I found this: ...
13
by: vashwath | last post by:
Hi all, To test the return value of system I wrote 2 programs which are shown below. Program 1: #include <stdio.h> #include <stdlib.h> int main(void)
5
by: Nomen Nescio | last post by:
I'm running gpg in python to verify a signature. I can see that it is working, because gpg is displaying the results. But I need a way to let the python script know this. According to the gpg...
1
by: Kevin Walzer | last post by:
I'm trying to display the output of an external process (invoked via popen) in a Tkinter text widget. I successfully start the process (based on what I'm seeing in my terminal output), but I can't...
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt" "http://search.cpan.org" and capture all it's...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.