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

Birch garyb at ea.com
Tue Oct 21 12:16:30 EDT 2003


"Paul Watson" <pwatson at redlinec.com> wrote in message news:<3f933ba4$1_1 at themost.net>...
> "Birch" <garyb at ea.com> wrote in message
> news:fb6e26e.0310141511.3bd42877 at posting.google.com...
> > 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!




More information about the Python-list mailing list