Get Stdout from external script startet through python

Pearu Peterson pearu at cens.ioc.ee
Sun Nov 17 18:11:31 EST 2002


On 16 Nov 2002, Christian Rapp wrote:

> I need to start an external script from inside python. This works just fine.
> The called script produces some output on StandardOut, that I need within
> the python-script. How do I get this Output into python?

As already mentioned by other authors in this thread, os.popen and
its extensions are the answer. Let me add here also commands.getoutput
(and its extensions) as another alternative.

But may I extend the question to extension modules, that is, how to get
the (stdout or stderr) output that originates, say, from a
fprintf(stdout, ...) statement in an Python/C extension module?

This questions has been raised also in f2py-users mailing list where
there was a wish to catch the stdout of a Fortran subroutine, wrapped to
Python. So far I have only an idea that this could be achieved
1) by determining the pty (with os.ttyname(sys.stdout.fileno())) where
these messages go;
2) and then using socket and termios modules to get the Fortran or C
messages.
How to get 2) to work, I haven't figured out yet. But it seems to be 
possible, in principle. For example, ttysnoop does this. 

So, any hints how to do that using Python tools and as an ordinary user
(e.g. ttysnoop requires changes to /etc/inittab but it has other goals..),
would be appreciated.

Pearu






More information about the Python-list mailing list