By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.