Custom Formatting The Output Of subprocess.Popen

thedsadude at thedsadude at
Fri Nov 21 20:10:53 CET 2008

On Nov 21, 8:50 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> thedsad... at wrote:
> >   Hello,
> >   I'm launching a script as follows:
> > <code>
> > p = subprocess.Popen(['./', 'aa'])
> > p.wait()
> > </code>
> >   If writes to sys.stdout, then it is shown on the console....
> You seem to be missing the fact that ./py is run in a different process.
> The "sys.stdout" that uses is different from that in the program
> calling Popen.  In fact, it could be using a different Python.  The
> situation is really similar to
>      p = subprocess.Popen([<basic program>, 'aa'])
> in that you have no way to "muck with the guts" of the subprocess, you
> can only post-process its output.
> --Scott David Daniels
> Scott.Dani... at Acm.Org


  Thanks Diez & Scott for your replies.

  The subprocess.Popen documentation states, concerning the stding,
stdout, stderr, arguments, that:
stdin, stdout and stderr specify the executed programs' standard
input, standard output and standard error file handles, respectively.
Valid values are PIPE, an existing file descriptor (a positive
integer), an existing file object, and None. PIPE indicates that a new
pipe to the child should be created. With None, no redirection will
occur; the child's file handles will be inherited from the parent.
Additionally, stderr can be STDOUT, which indicates that the stderr
data from the applications should be captured into the same file
handle as for stdout.
  so, it seems to me that if I would know how to write a file object,
then I could write one that prefixes each line, and that would be
fine, no? I don't see how this would necessitate waiting for's
termination, or matter that it is a different process. I just don't
know how to create a file object.

  Thanks & Bye,


More information about the Python-list mailing list