473,583 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15086
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.getout put (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.or g>:
| 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.getout put (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('ne wdirectory/make.log')

Donn Cave, do**@drizzle.co m
Jul 18 '05 #3
Donn Cave wrote:
Quoth mackstann <ma**@incise.or g>:
| 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.getout put (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('ne wdirectory/make.log')

Donn Cave, do**@drizzle.co m

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.getout put (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.getout put (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.getout put (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.getout put (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.wr ite('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
12696
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 issue is that I redirect all of the output from this script to a file (including that which is printed by the spawned programs) via the redirect (">") function on a Win2K Command Prompt. In the...
4
1919
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 out how to do such. Here is what I got... class cStdOutRedirect : stdOld = "" def __main__(self) : stdOld = sys.stdout
4
404
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 link OR know the trick, please let me know. // result contains the stdout of the command char* result=System("ls -l"); char* result=System("cat myFile.txt");
20
13102
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 system( ) sends to stdout ? (is there a simpler way than having to fork a child process and then catching its output ?) (if needed: working on linux) Thanks
2
3595
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 it with a StringBuilder and setting the Console.Out to the String Writer, when ever the RichEdit is to be updated text from the StringBuilder is being copied. The Problem:
18
1659
by: v4vijayakumar | last post by:
is there any standard way to extract output from system(3) function? TIA.
1
3761
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 VS2005 C/C++ compiler itself! I would like to capture the results of the compile and display them in a RichTextBox. The problem I'm having is that when I intentionally introduce an error in the C...
6
2822
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 will be executed. for new_folder, old_folder in folder_array: os.system('MD "' + new_folder + '"'); os.system('XCOPY "' + old_folder + '" "' + new_folder + '"');
0
1820
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, originally my code was developed for Linux and that did not require redirection due to the fact that every X11 application can have an STDOUT associated with it. I then proceeded to take interest in...
2
4673
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( 'toto', 'w' ) sys.stdout = fd os.system( "ls" )
0
7893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8172
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. ...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
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...
0
6574
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...
1
5695
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...
0
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2326
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
0
1151
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...

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.