test_count = 0
while test_count <= 100:
print test_count
test_count +=1
目前该计数器正在下一行打印,但我正在寻找将其覆盖在“0”上的方法。
test_count = 0
while test_count <= 100:
print test_count
test_count +=1
目前该计数器正在下一行打印,但我正在寻找将其覆盖在“0”上的方法。
将\r
回车符与sys.stdout.flush
.
import sys
import time # for invoking time.sleep(n_seconds) inside loop
counter = 0
while counter <= 100:
time.sleep(1)
counter += 1
sys.stdout.write("\rTesting (%ss elapsed)" % counter)
sys.stdout.flush()
Use \r
and add a ,
at the end of your print statement to not automatically write a newline as in the code below.
Also, see the python-progressbar library for some nice text implementations of progress bars.
import time # Added to demonstrate effect
test_count = 0
while test_count <= 100:
print "\r%3d" % test_count,
time.sleep(0.1)
test_count +=1
您应该包括以下语句: 首先在文件开头导入“os”模块:
import os
然后在循环末尾添加:
os.system('cls')
希望,这对你有帮助:)