[Tutor] captering output of a cmd run using os.system.

GADGIL PRASAD /INFRA/INFOTECH GADGILP@INFOTECH.ICICI.com
Fri, 1 Jun 2001 14:46:54 +0530


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C0EA7B.99B448A0
Content-Type: text/plain;
	charset="iso-8859-1"


hi,

that works! thanks daniel, roeland, lindsey.

regards,
prasad

> -----Original Message-----
> From: Daniel Yoo [mailto:dyoo@hkn.eecs.berkeley.edu]
> Sent: Friday, June 01, 2001 2:25 PM
> To: GADGIL PRASAD /INFRA/INFOTECH
> Cc: 'tutor@python.org'
> Subject: Re: [Tutor] captering output of a cmd run using os.system.
> 
> 
> On Fri, 1 Jun 2001, GADGIL PRASAD     /INFRA/INFOTECH wrote:
> 
> > a cmd that I run using os.sustem() gives the output that I want to
> > assign to a var and use later in prog. How do I do that ?
> 
> Ah, that's where you'll want to use os.popen().  It does 
> pretty much the
> same thing as os.system() --- it executes an external command from the
> system.  The only difference is that it allows us to grab the output
> directly.
> 
> 
> For example, if we wanted to store the output of a 'dir' 
> command, we can
> do something like this:
> 
> ###
> # Small program to demonstrate os.popen().
> import os
> 
> f = os.popen('dir')
> output = f.read()
> 
> print "Here's what we got back:", output
> ###
> 
> 
> What os.popen() does is return something that looks like a 
> file: we can
> read() from it in one sitting, or pull out lines at a time with
> readline().  os.popen() is mentioned in the reference docs here:
> 
>     http://python.org/doc/current/lib/os-newstreams.html
> 
> 
> If you're curious, you might want to see what sort of things 
> we'd expect
> files to do:
> 
>     http://python.org/doc/current/lib/bltin-file-objects.
> 
> 
> Good luck!
> 


. 


------_=_NextPart_001_01C0EA7B.99B448A0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>RE: [Tutor] captering output of a cmd run using =
os.system.</TITLE>
</HEAD>
<BODY>
<BR>

<P><FONT SIZE=3D2>hi,</FONT>
</P>

<P><FONT SIZE=3D2>that works! thanks daniel, roeland, lindsey.</FONT>
</P>

<P><FONT SIZE=3D2>regards,</FONT>
<BR><FONT SIZE=3D2>prasad</FONT>
</P>

<P><FONT SIZE=3D2>&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=3D2>&gt; From: Daniel Yoo [<A =
HREF=3D"mailto:dyoo@hkn.eecs.berkeley.edu">mailto:dyoo@hkn.eecs.berkeley=
.edu</A>]</FONT>
<BR><FONT SIZE=3D2>&gt; Sent: Friday, June 01, 2001 2:25 PM</FONT>
<BR><FONT SIZE=3D2>&gt; To: GADGIL PRASAD /INFRA/INFOTECH</FONT>
<BR><FONT SIZE=3D2>&gt; Cc: 'tutor@python.org'</FONT>
<BR><FONT SIZE=3D2>&gt; Subject: Re: [Tutor] captering output of a cmd =
run using os.system.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; On Fri, 1 Jun 2001, GADGIL =
PRASAD&nbsp;&nbsp;&nbsp;&nbsp; /INFRA/INFOTECH wrote:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; &gt; a cmd that I run using os.sustem() gives =
the output that I want to</FONT>
<BR><FONT SIZE=3D2>&gt; &gt; assign to a var and use later in prog. How =
do I do that ?</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Ah, that's where you'll want to use =
os.popen().&nbsp; It does </FONT>
<BR><FONT SIZE=3D2>&gt; pretty much the</FONT>
<BR><FONT SIZE=3D2>&gt; same thing as os.system() --- it executes an =
external command from the</FONT>
<BR><FONT SIZE=3D2>&gt; system.&nbsp; The only difference is that it =
allows us to grab the output</FONT>
<BR><FONT SIZE=3D2>&gt; directly.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; For example, if we wanted to store the output =
of a 'dir' </FONT>
<BR><FONT SIZE=3D2>&gt; command, we can</FONT>
<BR><FONT SIZE=3D2>&gt; do something like this:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; ###</FONT>
<BR><FONT SIZE=3D2>&gt; # Small program to demonstrate =
os.popen().</FONT>
<BR><FONT SIZE=3D2>&gt; import os</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; f =3D os.popen('dir')</FONT>
<BR><FONT SIZE=3D2>&gt; output =3D f.read()</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; print &quot;Here's what we got back:&quot;, =
output</FONT>
<BR><FONT SIZE=3D2>&gt; ###</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; What os.popen() does is return something that =
looks like a </FONT>
<BR><FONT SIZE=3D2>&gt; file: we can</FONT>
<BR><FONT SIZE=3D2>&gt; read() from it in one sitting, or pull out =
lines at a time with</FONT>
<BR><FONT SIZE=3D2>&gt; readline().&nbsp; os.popen() is mentioned in =
the reference docs here:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; <A =
HREF=3D"http://python.org/doc/current/lib/os-newstreams.html" =
TARGET=3D"_blank">http://python.org/doc/current/lib/os-newstreams.html</=
A></FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; If you're curious, you might want to see what =
sort of things </FONT>
<BR><FONT SIZE=3D2>&gt; we'd expect</FONT>
<BR><FONT SIZE=3D2>&gt; files to do:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; <A =
HREF=3D"http://python.org/doc/current/lib/bltin-file-objects" =
TARGET=3D"_blank">http://python.org/doc/current/lib/bltin-file-objects</=
A>.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Good luck!</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
</P>
<BR>

<P><B><FONT SIZE=3D2>.. </FONT></B>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C0EA7B.99B448A0--