473,513 Members | 13,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stdout funniness from os.system() calls when redirecting output

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 captured
output however, the output from the os.system() calls ALWAYS comes
before the output from the print calls in the python script.

This does NOT happen if I run the python script without redirecting
the output to a file. (everything prints out properly in the Command
Prompt window)

Does anyone have any experience with this, and/or know the fix?

Much Appreciated!
Jul 18 '05 #1
2 12690

"Birch" <ga***@ea.com> wrote in message
news:fb*************************@posting.google.co m...
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 captured
output however, the output from the os.system() calls ALWAYS comes
before the output from the print calls in the python script.

This does NOT happen if I run the python script without redirecting
the output to a file. (everything prints out properly in the Command
Prompt window)


You probably need to flush what is in the buffers of the parent process
before calling os.system(). Then, do not attempt redirection on the
os.system() command. The stdout file handle should be passed to the child.
This worked on AIX under Python 2.1.

#! /usr/bin/env python
import os
import sys
print "python is started"
sys.stdout.flush()
os.system('ls -al')
print "python is done"
sys.stdout.flush()

However, it appears that Win32 Python 2.3 may not be passing the the file
descriptor or something else is awry. Is this a bug?

C:\src\t>type pt.py
#! /usr/bin/env python
import os
print "python is started"
os.system('dir')
print "python is done"

C:\src\t>pt.py
python is started
Volume in drive C is dir
Volume Serial Number is 146D-04D8

Directory of C:\src\t

2003-10-19 20:24 <DIR> .
2003-10-19 20:24 <DIR> ..
2003-10-19 20:25 35 jj
2003-10-19 20:20 104 pt.py
2 File(s) 139 bytes
2 Dir(s) 4,800,147,456 bytes free
python is done

C:\src\t>pt.py >jj
There is not enough space on the disk.
Jul 18 '05 #2
"Paul Watson" <pw*****@redlinec.com> wrote in message news:<3f**********@themost.net>...
"Birch" <ga***@ea.com> wrote in message
news:fb*************************@posting.google.co m...
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 captured
output however, the output from the os.system() calls ALWAYS comes
before the output from the print calls in the python script.

This does NOT happen if I run the python script without redirecting
the output to a file. (everything prints out properly in the Command
Prompt window)


You probably need to flush what is in the buffers of the parent process
before calling os.system(). Then, do not attempt redirection on the
os.system() command. The stdout file handle should be passed to the child.
This worked on AIX under Python 2.1.

#! /usr/bin/env python
import os
import sys
print "python is started"
sys.stdout.flush()
os.system('ls -al')
print "python is done"
sys.stdout.flush()

However, it appears that Win32 Python 2.3 may not be passing the the file
descriptor or something else is awry. Is this a bug?

C:\src\t>type pt.py
#! /usr/bin/env python
import os
print "python is started"
os.system('dir')
print "python is done"

C:\src\t>pt.py
python is started
Volume in drive C is dir
Volume Serial Number is 146D-04D8

Directory of C:\src\t

2003-10-19 20:24 <DIR> .
2003-10-19 20:24 <DIR> ..
2003-10-19 20:25 35 jj
2003-10-19 20:20 104 pt.py
2 File(s) 139 bytes
2 Dir(s) 4,800,147,456 bytes free
python is done

C:\src\t>pt.py >jj
There is not enough space on the disk.


sys.stdout.flush() seems to do it... now I can organize my output log
files to mean something to people other than myself!

Thanks so much!
Jul 18 '05 #3

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

Similar topics

8
15074
by: Terry Gray | last post by:
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...
1
5351
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
6064
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
4560
by: Jacek | last post by:
Hello! My application has to use external native library writing to stdout and stdin. My goal is to redirect output within running process (no chance to do that in child process - Process class...
2
3568
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...
9
18263
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
8
5763
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
0
1178
by: Thomas Schreiner | last post by:
Hi, I'm extending a windows application (C++) by embedding Python calls. It seems to be a known problem that windows applications detach immediately from the calling console, so that all output...
1
3757
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...
0
7153
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...
0
7373
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,...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
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,...
1
5079
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...
0
4743
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.