简体   繁体   English

python中的sys.stdout.write无法按预期工作

[英]sys.stdout.write in python not working as expected

Consider the following code snippet 考虑以下代码片段

#!/usr/bin/python3

import sh
import sys

sys.stdout.write("hello")
sh.whoami(_out=sys.stdout.buffer)

I expect the above program to print 我希望上面的程序可以打印

hello
username

but instead i get 但是我得到了

username
hello

Can anyone please explain me why this is happening? 谁能解释一下为什么会这样吗? I am using python 3.5.2 on linux mint 18.1 我在Linux Mint 18.1上使用python 3.5.2

That's output buffering for you. 这就是您的输出缓冲。 If you did: 如果您这样做:

sys.stdout.write("hello")
sys.stdout.flush()
sh.whoami(_out=sys.stdout.buffer)

the order would be as you expected. 订单将与您预期的一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM