擅长:python、mysql、java
<p>我发现使用<code>sys.stdout</code>是一种更独立于系统的方式来实现这一点,原因与<code>print</code>的工作方式有关。但是您必须显式地<code>flush</code>缓冲区,所以我把它放在一个函数中。在</p>
<pre><code>def carriage_return():
sys.stdout.write('\r')
sys.stdout.flush()
</code></pre>
<p>这算是一个玩笑。如果有帮助请告诉我。在</p>
<p>我试过了,对我有用。这个<code>time.sleep</code>只是为了戏剧化。在</p>
^{pr2}$
<p>最后,我看到人们也这样做。使用这样的终端控制代码在某些方面似乎是正确的,但对我来说,它似乎更脆弱。对于我来说,上面的代码同样适用(在OSX上)。在</p>
<pre><code>def carriage_return():
if sys.platform.lower().startswith('win'):
print '\r'
else:
print chr(27) + '[A'
</code></pre>