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