擅长:python、mysql、java
<p>注意:这里有一些微妙之处,包括流是否会流向交互设备。最令人惊讶的是,在Python 3 stderr中是行缓冲的(至少在Unix中是这样)。例如,在一个终端窗口中,以下内容在Python 2中每两秒钟打印一个数字:</p>
<pre><code>for n in range(5):
print >> sys.stderr, n, # final comma to squelch newline character
time.sleep(2)
</code></pre>
<p>而在Python 3中,当循环完成时,以下内容将所有数字一起打印:</p>
<pre><code>for n in range(5):
print(n, file=sys.stderr, end='') # print n to sys.stderr with no newline char
time.sleep(2)
</code></pre>