[英]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.