473,382 Members | 1,289 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,382 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 4104
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.