繁体   English   中英

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

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

考虑以下代码片段

#!/usr/bin/python3

import sh
import sys

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

我希望上面的程序可以打印

hello
username

但是我得到了

username
hello

谁能解释一下为什么会这样吗? 我在Linux Mint 18.1上使用python 3.5.2

这就是您的输出缓冲。 如果您这样做:

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

订单将与您预期的一样。

暂无
暂无

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

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