python程序中语句的执行顺序

时间:2017-12-28 10:55:29

标签: python python-3.x

我有一个python代码,如

for x in name:
     print(x)
     os.system('exe file')#prints output1
     os.system('2nd exe file)#prints output2

正在打印

output1
x
output2

为什么会这样?

1 个答案:

答案 0 :(得分:0)

这已被解决here。检查答案是否有解决方法。

  

当您输出到管道时,Python会缓冲您的输出   写入sys.stdout并在刷新后或之后输出   溢出,或关闭时(程序退出时)。虽然它会   缓冲打印调用,系统调用直接输出到stdout和   他们的输出不会被缓冲。这就是你看到这种情况的原因   优先。为避免这种情况,请使用python -u

python -u test.py > test.out; cat test.out
     

查看更多信息here

     

1:   Disable output buffering

相关问题