[Tutor] how to get response from os.system()
linuxian iandsd
pylinuxian at gmail.com
Sun Mar 16 20:17:08 CET 2008
i guess that was too short tutorial well, here is an example from a live
session on the interpreter.
[root at serv ~]# python
Python 2.5.1 (r251:54863, Nov 23 2007, 16:16:53)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> cmd='ping -c 1 localhost'
>>> import os
>>> a=os.popen(cmd)
>>> print a
<open file 'ping -c 1 localhost', mode 'r' at 0xb7f1c728>
>>> print a.read()
PING serv.kontactel.loc (127.0.0.1) 56(84) bytes of data.
64 bytes from serv.kontactel.loc (127.0.0.1): icmp_seq=1 ttl=64 time=0.134ms
--- serv.kontactel.loc ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.134/0.134/0.134/0.000 ms
>>>
i hope you can understand that now u have the output in a variable called
"a" that you can parse & use as you wish.
On Sun, Mar 16, 2008 at 7:12 PM, linuxian iandsd <pylinuxian at gmail.com>
wrote:
> use os.popen("your cmd here")
>
>
> On Sun, Mar 16, 2008 at 8:06 PM, Luke Paireepinart <rabidpoobear at gmail.com>
> wrote:
>
> > shawn bright wrote:
> > > Lo there all,
> > >
> > > I am needing to get a response back from a system command.
> > > i can do this:
> > >
> > > os.system('mailq | wc -l")
> > >
> > > if i do this in the terminal mailq | wc -l , it will spit out a
> > number.
> > > How do i get that number as a python variable ?
> > You need to use the subprocess module, specifically subprocess.Popen, to
> > open an input pipe from the command so you can read() the data in.
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080316/37384335/attachment.htm
More information about the Tutor
mailing list