Python script prints output of os.system before print
command-line, linux, python, stdout
Solution
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, use `python -u`:
python -u test.py > test.out; cat test.out
See more info here.
EDIT: explanation on when the buffer will be flushed.
Problem
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??