Behavior of os.system() with redirection

Mark Aristotle_00 at yahoo.com
Wed Jan 2 15:24:53 EST 2002


Well, I'm scratching my head over the following:

#!/usr/bin/python
import os
print "hi"
os.system("ls -l > results")
os.system("cat results")

Now, if I run the program, I get the following:

[mfenner at bacon tmp]$ ../calltest.py
hi
total 0
-rw-rw-r--    1 mfenner  mfenner         0 Jan  2 15:21 results
[mfenner at bacon tmp]$

which is all well and good.

However, if I redirect the output:
[mfenner at bacon tmp]$ ../calltest.py > out

and THEN look at the results:
[mfenner at bacon tmp]$ cat out
total 0
-rw-rw-r--    1 mfenner  mfenner         0 Jan  2 15:21 out
-rw-rw-r--    1 mfenner  mfenner         0 Jan  2 15:21 results
hi

I get something quite a bit different.  Now, I'm not concerned with
the extra file ("out") since that is an artifact of the redirection. 
However, I am concerned about the "hi" being AFTER the "ls" output in
one case and BEFORE it in the other.

What's going on here?  How can I get a uniform ("hi" always first)
behavior?

Regards,
Mark



More information about the Python-list mailing list