473,748 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Formatting The Output Of subprocess.Pope n

Hello,

I'm launching a script as follows:
<code>
p = subprocess.Pope n(['./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.Pope n(['./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 4131
th********@gmai l.com wrote:
Hello,

I'm launching a script as follows:
<code>
p = subprocess.Pope n(['./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.Pope n(['./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.Pope n(...)
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********@gmai l.com wrote:
Hello,

I'm launching a script as follows:
<code>
p = subprocess.Pope n(['./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.Pope n([<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***********@A cm.Org
Nov 21 '08 #3
On Nov 21, 8:50*pm, Scott David Daniels <Scott.Dani...@ Acm.Orgwrote:
thedsad...@gmai l.com wrote:
* Hello,
* I'm launching a script as follows:
<code>
p = subprocess.Pope n(['./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.Pope n([<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...@A cm.Org

Hello,

Thanks Diez & Scott for your replies.

The subprocess.Pope n 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********@gmai l.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(o bject):
def write(self, sometext):
print repr(sometext)
def close(self):
pass

outputter = MyFunkyOutput()
print >>outputter, 1,'a', 43
Nov 21 '08 #5
th********@gmai l.com wrote:
On Nov 21, 8:50 pm, Scott David Daniels <Scott.Dani...@ Acm.Orgwrote:
>thedsad...@gma il.com wrote:
>> Hello,
I'm launching a script as follows:
<code>
p = subprocess.Pope n(['./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.Pope n([<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.Pope n 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.pope n, 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
9461
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 the way. For example: tmp = os.popen("hostname").read() ....works as expected.
3
5506
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 of pywin32, but my trouble exists with either option: import subprocess process = subprocess.Popen(, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) stat = process.wait() print process.stdout.read()
6
17808
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 prevent from being displayed. Is there a way to prevent the output of x.exe from python?
9
9378
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
2010
by: johnny | last post by:
How do I pipe the output, generated from os.system(some_command), to the logging file?
14
7433
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 at least two different files. eg. stdout/stderr -screen
4
13537
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() subprocess.call("ls", stdout = file)
2
10130
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 work: #!/usr/bin/python import subprocess as proc import tempfile name = tempfile.NamedTemporaryFile(mode='w+b') print 'name is '+ name.name
25
2789
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 object itself is a subprocess, not a "popen". It seems that it would be more accurate to just name the class Subprocess, can anyone explain why this is not the case? Thank you.
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.