4

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??

TJ Shah
  • 435
  • 4
  • 17

3 Answers3

3

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.

Community
  • 1
  • 1
Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
3

Another way to prevent the os buffering is to flush the output after the first print:

#!/usr/bin/env python

import sys
print "first"
sys.stdout.flush()
import os
os.system("echo second")
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
0

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.

William Pursell
  • 204,365
  • 48
  • 270
  • 300