Python script prints output of os.system before pr

2019-01-24 18:28发布

问题:

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

回答1:

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.



回答2:

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")


回答3:

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.