[BangPypers] Reg : Python Scripting

Anand Balachandran Pillai abpillai at gmail.com
Mon Dec 14 07:46:30 CET 2009


On Mon, Dec 14, 2009 at 11:09 AM, murugadoss <murugadoss2884 at gmail.com>wrote:

> Hi,
>   Thank u for the reply. My program is like this;
>
> int i;
> printf("?");
> scanf("%d",&i);
>
> python script:
> #!/usr/bin/env python
> import sys
> import os
> import time
>
> os.system("./a.out")
> os.system("2")
>
> output:
> ./prog.py
> ?  ----> Waiting for input
>
> My input is given in the next line of the script.The control is waiting at
> the first line (os.system("./a.out")).
>

If you process needs input from stdin, os.system(...) is not how
to do it. Instead you should use os.popen(...) or for better control
the functions defined in subprocess module.

Since I don't want to type stuff again, refer to this recipe
which I submitted some time back. It illustrates different ways
of using subprocess module. You should take a look at
execute_command_in_pipe function.

http://code.activestate.com/recipes/498137/

For example, here is how to use subprocess to capture
output from directory listing of /tmp.

>>> from subprocess import *
>>> cmd=['ls','-la','/tmp']
>>> p=Popen(cmd, stdout=PIPE, stderr=PIPE)
>>> out, err=p.communicate()
>>> print out
total 76
drwxrwxrwt 14 root      root      4096 2009-12-14 12:04 .
drwxr-xr-x 25 root      root      4096 2009-12-09 20:02 ..
drwx------  2 anand     anand     4096 2009-12-10 18:47 emacs500
drwx------  2 anand     anand     4096 2009-12-09 20:03 .esd-500
-rw-r--r--  1 root      root      1466 2009-12-09 19:48
exporter_info_168764.xml
-rw-r--r--  1 root      root      1337 2009-12-09 19:50
exporter_info_563178.xml
srwxr-xr-x  1 root      root         0 2009-04-21 12:56
gnome-system-monitor.root.2075479178

Likewise you can construct code which can communicate
with stdin, stdout/stderr.


Regards

--Anand


> How shall i do this ?
>
> --
> Regards
> V.Murugadoss
>
>
> On Mon, Dec 14, 2009 at 2:10 PM, Noufal Ibrahim <noufal at gmail.com> wrote:
>
> > On Mon, Dec 14, 2009 at 10:04 AM, murugadoss <murugadoss2884 at gmail.com
> > >wrote:
> >
> > > Hello,
> > >   I am new to python scripting. I saw about subprocess module. I need
> to
> > > send an input to the running src,like
> > >
> > > eg: For adding two numbers
> > >       1. image running and waiting for input from user
> > >        2. input 1
> > >       3. input 2
> > >
> > > eg:
> > > ./add.src
> > > 2
> > > 3
> > > Can u please give me some more input how to pass there inputs
> >
> >
> >
> > You can write to the stdin of a running process. Did you look at the
> docs?
> > Check out the communicate method of the Popen objects. There's also this
> > that you might find useful
> > http://blog.doughellmann.com/2007/07/pymotw-subprocess.html
> >
> > --
> > ~noufal
> > http://nibrahim.net.in
> >  _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
--Anand


More information about the BangPypers mailing list