在再次覆盖同一行之前,您至少需要清除点有空格的位置。
def processing_flush(n, index=5):
sys.stdout.write("\rProcessing %s" % (index * " "))
sys.stdout.write("\rProcessing %s" % ((n % index)* "."))
sys.stdout.flush()
上面的代码可能会导致一些短暂的闪烁。在您的具体情况下,在以下情况下清除线路就足够了n % index
变为0:
def processing_flush(n, index=5):
if n % index == 0:
sys.stdout.write("\rProcessing %s" % (index * " "))
sys.stdout.write("\rProcessing %s" % ((n % index)* "."))
sys.stdout.flush()
或者更好的是总是写index-1
人物:
def processing_flush(n, index=5):
sys.stdout.write("\rProcessing %s%s" % ((n % index)* ".", (index - 1 - (n % index))* " "))
sys.stdout.flush()
Edit 1:或者,如果您希望光标始终位于最后一个点之后:
def processing_flush(n, index=5):
sys.stdout.write("\rProcessing %s%s" % ((n % index)* ".", (index - 1 - (n % index))* " "))
sys.stdout.write("\rProcessing %s" % ((n % index)* "."))
sys.stdout.flush()
Edit 2:或者,如果您希望光标始终位于行首:
def processing_flush(n, index=5):
sys.stdout.write("Processing %s%s\r" % ((n % index)* ".", (index - 1 - (n % index))* " "))
sys.stdout.flush()
原因是,如果您只覆盖前一行的第一部分,您的 shell 会记住前一行的剩余字符。