Python script prints output of os.system before pr

2019-01-24 17:44发布

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

3条回答
何必那么认真
2楼-- · 2019-01-24 18:20

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-24 18:22

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")
查看更多
仙女界的扛把子
4楼-- · 2019-01-24 18:22

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.

查看更多
登录 后发表回答