473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Custom Formatting The Output Of subprocess.Popen

Hello,

I'm launching a script as follows:
<code>
p = subprocess.Popen(['./p.py', 'aa'])

p.wait()
</code>

If p.py writes to sys.stdout, then it is shown on the console.
Looking at the console, then, it is hard to distinguish the output of
p.py from that of the script launching it. I'd like to do it so the
output looks like this:
<output>
output of launcher
p: output of launchee
p: more output of launchee
more output of launcher
</output>
i.e., that each output line of p.py will be formatted so that it is
preceded by 'p:'.

How should this be done, then? Should I write a file class, and pass
on object like so:
<code>
custom_f = custom_file(sys.stdout, line_prefix = 'p')

p = subprocess.Popen(['./p.py', 'aa'], stdout = custom_f)

p.wait()
</code>
or is a different option easier? Are there any links for writing
custom file classes?

Thanks & Bye,

TD

Nov 21 '08 #1
5 4101
th********@gmail.com wrote:
Hello,

I'm launching a script as follows:
<code>
p = subprocess.Popen(['./p.py', 'aa'])

p.wait()
</code>

If p.py writes to sys.stdout, then it is shown on the console.
Looking at the console, then, it is hard to distinguish the output of
p.py from that of the script launching it. I'd like to do it so the
output looks like this:
<output>
output of launcher
p: output of launchee
p: more output of launchee
more output of launcher
</output>
i.e., that each output line of p.py will be formatted so that it is
preceded by 'p:'.

How should this be done, then? Should I write a file class, and pass
on object like so:
<code>
custom_f = custom_file(sys.stdout, line_prefix = 'p')

p = subprocess.Popen(['./p.py', 'aa'], stdout = custom_f)

p.wait()
</code>
or is a different option easier? Are there any links for writing
custom file classes?
Why can't you just do?

print "<output>"
p = subprocess.Popen(...)
p.wait()
print "</output>"

? If you wait for the subprocess to terminate anyway..

Another way would be to use the p.communicate()-call to get the child's
output, and print that to stdout yourself, pre/postfixing it as needed.

Diez
Nov 21 '08 #2
th********@gmail.com wrote:
Hello,

I'm launching a script as follows:
<code>
p = subprocess.Popen(['./p.py', 'aa'])

p.wait()
</code>

If p.py 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 p.py 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
Sc***********@Acm.Org
Nov 21 '08 #3
On Nov 21, 8:50*pm, Scott David Daniels <Scott.Dani...@Acm.Orgwrote:
thedsad...@gmail.com wrote:
* Hello,
* I'm launching a script as follows:
<code>
p = subprocess.Popen(['./p.py', 'aa'])
p.wait()
</code>
* If p.py 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 p.py 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...@Acm.Org

Hello,

Thanks Diez & Scott for your replies.

The subprocess.Popen documentation states, concerning the stding,
stdout, stderr, arguments, that:
<quote>
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.
</quote>
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 p.py's
termination, or matter that it is a different process. I just don't
know how to create a file object.

Thanks & Bye,

TD

Nov 21 '08 #4
th********@gmail.com wrote:
...
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 p.py's
termination, or matter that it is a different process. I just don't
know how to create a file object.
Duck typing to the rescue: Here's a simple file object for writing:

class MyFunkyOutput(object):
def write(self, sometext):
print repr(sometext)
def close(self):
pass

outputter = MyFunkyOutput()
print >>outputter, 1,'a', 43
Nov 21 '08 #5
th********@gmail.com wrote:
On Nov 21, 8:50 pm, Scott David Daniels <Scott.Dani...@Acm.Orgwrote:
>thedsad...@gmail.com wrote:
>> Hello,
I'm launching a script as follows:
<code>
p = subprocess.Popen(['./p.py', 'aa'])
p.wait()
</code>
If p.py 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 p.py 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...@Acm.Org


Hello,

Thanks Diez & Scott for your replies.

The subprocess.Popen documentation states, concerning the stding,
stdout, stderr, arguments, that:
<quote>
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.
</quote>
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 p.py's
termination, or matter that it is a different process. I just don't
know how to create a file object.
It was an astute observation that you could provide your own "file-like
object" as the output for the subprocess. However, you may find that due
to buffering effects the subprocess's output doesn't get completely
intermingled with the calling process's output.

Although you can specify "unbuffered", which is the default for
subprocess.popen, I don't believe this guarantees that the subprocess
itself won't perform buffering. Others may know better.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 21 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: jas | last post by:
I would like to redirect the output from os.system to a variable, but am having trouble. I tried using os.popen(..).read() ...but that doesn't give me exactly what i want. ...this is windows by...
3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
6
by: Kkaa | last post by:
I'm using the os.system command in a python script on Windows to run a batch file like this: os.system('x.exe') The third-party program x.exe outputs some text to the console that I want to...
9
by: Clodoaldo Pinto Neto | last post by:
Output from the shell: $ set | grep IFS IFS=$' \t\n' Output from subprocess.Popen(): "IFS=' \t\n" Both outputs for comparison:
3
by: johnny | last post by:
How do I pipe the output, generated from os.system(some_command), to the logging file?
14
by: nathan.shair | last post by:
Hello, I searched on Google and in this Google Group, but did not find any solution to my problem. I'm looking for a way to output stdout/stderr (from a subprocess or spawn) to screen and to...
4
by: Tobiah | last post by:
I am not sure how to capture the output of a command using subprocess without creating a temp file. I was trying this: import StringIO import subprocess file = StringIO.StringIO() ...
2
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.