Capturing stdout from a class method

Thomas Jollans thomas at
Wed Jun 27 21:48:53 CEST 2007

Falcolas wrote:
> I have a rather strange situation, and I'm not sure my brief
> experience of Python will let me handle it properly.
> The situation is this: I have a Java class "X" which I need to call in
> a Jython script. The  output of "X" is sent to stdout using the java
> call System.out. I need to capture this output, and preform tests on
> it in the Jython script.
> My first pass at a design involves two jython scripts. One (we'll call
> it "Y") whose sole function is to instantiate and call "X", and die.
> The second script will call "Y" using a method which captures stdout
> to a pipe. The second script would then read the stdout from the pipe
> and act accordingly.
> Can anybody suggest a better way to handle this? The Java class, "X",
> can not be modified for this test.

How about subprocess.Popen ? I'm thinking of something like this:

import subprocess

X_proc = subprocess.Popen(['java', 'Xstarter'])
for line in Xproc.stdout:
   # do funky stuff

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the Python-list mailing list