472,328 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

os.system stdout redirection...

Using Python 2.2 in Debian linuxI am trying to change to a different
directory, execute a 'make all' command, and redirect the output of the
subshell to a PyQt window... I should be able to execute the
os.system('cd newdirectory; make all'), but how do I redirect stdout of
the new subshell created by the os.system call?

Any help would be appreciated.

twgray

Jul 18 '05 #1
8 15000
On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote:
Using Python 2.2 in Debian linuxI am trying to change to a different
directory, execute a 'make all' command, and redirect the output of the
subshell to a PyQt window... I should be able to execute the
os.system('cd newdirectory; make all'), but how do I redirect stdout of
the new subshell created by the os.system call?

Any help would be appreciated.


You can use os.popen (popen2 and 3 as well), or the popen2 module's
Popen3 and 4 classes, or commands.getoutput (and there are probably even
more ways :).

--
m a c k s t a n n mack @ incise.org http://incise.org
While having never invented a sin, I'm trying to perfect several.

Jul 18 '05 #2
Quoth mackstann <ma**@incise.org>:
| On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote:
|> Using Python 2.2 in Debian linuxI am trying to change to a different
|> directory, execute a 'make all' command, and redirect the output of the
|> subshell to a PyQt window... I should be able to execute the
|> os.system('cd newdirectory; make all'), but how do I redirect stdout of
|> the new subshell created by the os.system call?

| You can use os.popen (popen2 and 3 as well), or the popen2 module's
| Popen3 and 4 classes, or commands.getoutput (and there are probably even
| more ways :).

Yes, very good, there are many ways to redirect output, but you
need to know how to make PyQt monitor a file and copy it to a window
before it matters much either way.

I don't know, so I've changed the subject line, replacing the elipsis
("..." - what was that for?) with [to PyQt window] to attract the
attention of someone who might have a clue.

Do you need to write this output line by line as it comes out of make?
Or would it be fine to run make, and then wait to present all the output
after it's finished? The latter is likely to be significantly easier.

# make output goes to both units 1 and 2 (output and error/diagnostic)
os.system('cd newdirectory; make all > make.log 2>&1')
displayfile('newdirectory/make.log')

Donn Cave, do**@drizzle.com
Jul 18 '05 #3
Donn Cave wrote:
Quoth mackstann <ma**@incise.org>:
| On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote:
|> Using Python 2.2 in Debian linuxI am trying to change to a different
|> directory, execute a 'make all' command, and redirect the output of the
|> subshell to a PyQt window... I should be able to execute the
|> os.system('cd newdirectory; make all'), but how do I redirect stdout of
|> the new subshell created by the os.system call?

| You can use os.popen (popen2 and 3 as well), or the popen2 module's
| Popen3 and 4 classes, or commands.getoutput (and there are probably even
| more ways :).

Yes, very good, there are many ways to redirect output, but you
need to know how to make PyQt monitor a file and copy it to a window
before it matters much either way.

I don't know, so I've changed the subject line, replacing the elipsis
("..." - what was that for?) with [to PyQt window] to attract the
attention of someone who might have a clue.

Do you need to write this output line by line as it comes out of make?
Or would it be fine to run make, and then wait to present all the output
after it's finished? The latter is likely to be significantly easier.

# make output goes to both units 1 and 2 (output and error/diagnostic)
os.system('cd newdirectory; make all > make.log 2>&1')
displayfile('newdirectory/make.log')

Donn Cave, do**@drizzle.com

I need to display make's output line by line, since it a very long
compile cycle (kernel compile). Thanks for the help.

Jul 18 '05 #4
mackstann wrote:
On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote:
Using Python 2.2 in Debian linuxI am trying to change to a different
directory, execute a 'make all' command, and redirect the output of the
subshell to a PyQt window... I should be able to execute the
os.system('cd newdirectory; make all'), but how do I redirect stdout of
the new subshell created by the os.system call?

Any help would be appreciated.

You can use os.popen (popen2 and 3 as well), or the popen2 module's
Popen3 and 4 classes, or commands.getoutput (and there are probably even
more ways :).

All the Python docs I've been looking at must have been pre-2.0, because
this is the first I've heard of the popen2/3/Popen3/4 calls. Anyway, is
there a recommended way of capturing 'make's' output, line by line, and
redirecting it to a PyQt window? The window in question is a QTextEdit
control with a 'def write' function.

Again, thanks for the help.

Jul 18 '05 #5
On Sun, Aug 17, 2003 at 02:43:44PM -0500, Terry Gray wrote:
mackstann wrote:
On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote: You can use os.popen (popen2 and 3 as well), or the popen2 module's
Popen3 and 4 classes, or commands.getoutput (and there are probably even
more ways :).

All the Python docs I've been looking at must have been pre-2.0, because
this is the first I've heard of the popen2/3/Popen3/4 calls. Anyway, is
there a recommended way of capturing 'make's' output, line by line, and
redirecting it to a PyQt window? The window in question is a QTextEdit
control with a 'def write' function.

Again, thanks for the help.


Probably the simplest way is something like:

import os
prog = os.popen("echo hello")
print prog.read()

--> 'hello\n'

It's basically a file-like interface for running shell commands. You
can also open with "w" or "rw" and write to the command's stdin, or you
can use popen2/3/4 to have individual descriptors for stdin / stdout /
stderr. The popen2 module seems to be less cross-platform, at least
with regard to the Popen3/4 classes, as I see this in popen2.py:

if sys.platform[:3] == "win":
# Some things don't make sense on non-Unix platforms.
del Popen3, Popen4

But if you plan on only using unix, then Popen3/4 are kinda nice, if you
like a more OOPey interface, or want more process management abilities.
Example:

import popen2
prog = popen2.Popen3("echo hello; read i; echo $i")
print prog.fromchild.read()

--> 'hello\n'

There's also .tochild, to write to its stdin, and Popen4 has childerr,
for reading stderr. You can also do prog.poll() and prog.wait(), if you
need to check if it's still running, or wait for it to exit, and you can
get its pid via prog.pid.

So it kinda depends on whether you need to read from the command as
you're doing something else, or you want to just wait for it all to come
out at once.

import popen2

prog = popen2.Popen3("make spaghetti 2>&1")
output = ""

while 1:
text = prog.read()
if text:
output += text

At least, I'm pretty sure that's how you detect that the program is done
(reading ''). I've only used Popen3 to interface with mpg321, and it
sends a little quit message when it's done, and then I close it, so I
haven't had to check for when it exits.

If you need to read bits from it while you're simultaneously doing other
things, you can use prog.fromchild.fileno() with select.select(), for
example. Or launch a thread, or other things I'm sure.

--
m a c k s t a n n mack @ incise.org http://incise.org
My weight is perfect for my height -- which varies

Jul 18 '05 #6
mackstann wrote:
On Sun, Aug 17, 2003 at 02:43:44PM -0500, Terry Gray wrote:
mackstann wrote:
On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote:
You can use os.popen (popen2 and 3 as well), or the popen2 module's
Popen3 and 4 classes, or commands.getoutput (and there are probably even
more ways :).


All the Python docs I've been looking at must have been pre-2.0, because
this is the first I've heard of the popen2/3/Popen3/4 calls. Anyway, is
there a recommended way of capturing 'make's' output, line by line, and
redirecting it to a PyQt window? The window in question is a QTextEdit
control with a 'def write' function.

Again, thanks for the help.

Probably the simplest way is something like:

import os
prog = os.popen("echo hello")
print prog.read()

--> 'hello\n'

It's basically a file-like interface for running shell commands. You
can also open with "w" or "rw" and write to the command's stdin, or you
can use popen2/3/4 to have individual descriptors for stdin / stdout /
stderr. The popen2 module seems to be less cross-platform, at least
with regard to the Popen3/4 classes, as I see this in popen2.py:

if sys.platform[:3] == "win":
# Some things don't make sense on non-Unix platforms.
del Popen3, Popen4

But if you plan on only using unix, then Popen3/4 are kinda nice, if you
like a more OOPey interface, or want more process management abilities.
Example:

import popen2
prog = popen2.Popen3("echo hello; read i; echo $i")
print prog.fromchild.read()

--> 'hello\n'

There's also .tochild, to write to its stdin, and Popen4 has childerr,
for reading stderr. You can also do prog.poll() and prog.wait(), if you
need to check if it's still running, or wait for it to exit, and you can
get its pid via prog.pid.

So it kinda depends on whether you need to read from the command as
you're doing something else, or you want to just wait for it all to come
out at once.

import popen2

prog = popen2.Popen3("make spaghetti 2>&1")
output = ""

while 1:
text = prog.read()
if text:
output += text

At least, I'm pretty sure that's how you detect that the program is done
(reading ''). I've only used Popen3 to interface with mpg321, and it
sends a little quit message when it's done, and then I close it, so I
haven't had to check for when it exits.

If you need to read bits from it while you're simultaneously doing other
things, you can use prog.fromchild.fileno() with select.select(), for
example. Or launch a thread, or other things I'm sure.

Many thanks. That about covers all I needed.

Jul 18 '05 #7
Erwin S. Andreasen wrote:
Terry Gray <tg***@cox-internet.com> writes:

Using Python 2.2 in Debian linuxI am trying to change to a different
directory, execute a 'make all' command, and redirect the output of
the subshell to a PyQt window... I should be able to execute the
os.system('cd newdirectory; make all'), but how do I redirect stdout
of the new subshell created by the os.system call?

If you are going to use QT, Use QProcess. You can set it up to fire
off a signal when new output has arrived and then read it and update
your window.

Thanks, exactly what I was looking for.

Jul 18 '05 #8

Originally posted by Mackstann
On Sun, Aug 17, 2003 at 02:43:44PM -0500, Terry Gray wrote:
mackstann wrote:
On Sun, Aug 17, 2003 at 01:01:41AM -0500, Terry Gray wrote:

You can use os.popen (popen2 and 3 as well), or the popen2 module's

Popen3 and 4 classes, or commands.getoutput (and there are probably even

more ways :).
All the Python docs I've been looking at must have been pre-2.0,

because

this is the first I've heard of the popen2/3/Popen3/4 calls.

Anyway, is

there a recommended way of capturing 'make's' output, line by

line, and

redirecting it to a PyQt window? The window in question is a

QTextEdit

control with a 'def write' function. Again, thanks for the help.

Probably the simplest way is something like: import os prog = os.popen("echo hello") print prog.read() --> 'hello\n' It's basically a file-like interface for running shell commands. You can also open with "w" or "rw" and write to the command's
stdin, or you can use popen2/3/4 to have individual descriptors for stdin / stdout / stderr. The popen2 module seems to be less cross-platform, at least with regard to the Popen3/4 classes, as I see this in popen2.py: if sys.platform[:3] == "win": # Some things don't make sense on non-Unix platforms. del Popen3, Popen4 But if you plan on only using unix, then Popen3/4 are kinda
nice, if you like a more OOPey interface, or want more process management
abilities. Example: import popen2 prog = popen2.Popen3("echo hello; read i; echo $i") print prog.fromchild.read() --> 'hello\n' There's also .tochild, to write to its stdin, and Popen4 has childerr, for reading stderr. You can also do prog.poll() and
prog.wait(), if you need to check if it's still running, or wait for it to exit,
and you can get its pid via prog.pid. So it kinda depends on whether you need to read from the command as you're doing something else, or you want to just wait for it
all to come out at once. import popen2 prog = popen2.Popen3("make spaghetti 2>&1") output = "" while 1: text = prog.read() if text: output += text At least, I'm pretty sure that's how you detect that the
program is done (reading ''). I've only used Popen3 to interface with mpg321, and it sends a little quit message when it's done, and then I close it, so I haven't had to check for when it exits. If you need to read bits from it while you're simultaneously
doing other things, you can use prog.fromchild.fileno() with select.select(), for example. Or launch a thread, or other things I'm sure. -- m a c k s t a n n mack @ incise.org
http://incise.org/http://incise.org My weight is perfect for my height -- which varies


hi mackstann, hi everybody,

i hope i'm doing the right thing posting into this thread with that
little different topic, but i googled here and everything here seems
quite close to my problem:

i tried to use the ideas and code posted in this thread to control
mpg321. i send the 'LOAD' message to mpg321 via prog.write() but it
refuses to play audio until the whole script is finished or arborted
i.e. due to an error.

on top of that, the script gets stuck at the 'read()' statement wich i
try to use for fetching mpg321's feedback (current frame, end of file,
etc). here's the code:

prog = popen2.Popen3("mpg321 -R dummyargument")

prog.tochild.write('LOAD test.mp3\n')

print prog.fromchild.read()

mackstann, you said you were using popen to control mpg321. please tell
me how i can get it done...?

thanks for your time

baiste
--
Posted via http://dbforums.com
Jul 18 '05 #9

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

Similar topics

2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My...
4
by: Wes S. | last post by:
How can I redirect and tag sys.stdout values. I tried creating a class module that saved the old stdout and replaced it, but I can't seem to figure...
4
by: NewYorker | last post by:
Hi, I'm looking for code to do the following. Bascially, "System" shell out and execute the command and return the stdout in result. You know a...
20
by: SR | last post by:
Hi, I need to read the output from a system( ) function within a C program. The function however only returns the exit status. How can I read what...
2
by: Nadav | last post by:
Hi, Introduction: *************** I am trying to redirect stdout to a RichEdit control, this is done by initiating a StringWriter, associated...
18
by: v4vijayakumar | last post by:
is there any standard way to extract output from system(3) function? TIA.
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a C# application in which I start another process which produces output to stdout and stderr. In fact, that process is the uSoft...
6
by: stanleyxu | last post by:
Hi All, I am porting Perl script to Python script. Everything works fines until calling os.system(). In my script, a number of DOS-commands...
0
by: Tom Gaudasinski | last post by:
Greetings, I'm trying to redirect python's stdout to another location. The reason for this is that I'm embedding python in an application. Now,...
2
by: TP | last post by:
Hi everybody, The following code does not redirect the output of os.system("ls") in a file: import sys, os saveout = sys.stdout fd = open(...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.