I have a python script test.py:
print "first"
import os
os.system("echo second")
On linux command line I execute
python test.py
which returns:
first
second
I then execute
python test.py > test.out; cat test.out
which returns
second
first
What about redirecting the output makes the os.system call print before the print statement??
When you're outputting to a pipe, Python buffers your output that's written to
sys.stdout
and outputs it after a flush, or after it's overflown, or upon closing (when the program exits). While it'll buffer the print calls, system calls output directly into stdout and their output won't be buffered. That's why you're seeing such precedence. To avoid that, usepython -u
:See more info here.
EDIT: explanation on when the buffer will be flushed.
Another way to prevent the os buffering is to flush the output after the first print:
When the output of the python script is a tty, its output is line buffered. When the output is a regular file, the output is block buffered.