一个简单的解决方案就是写"\r"在字符串之前,而不添加换行符;如果字符串永远不会变短,这就足够了...sys.stdout.write("\rDoing thing %i" % i)
sys.stdout.flush()
稍微复杂一点的是进度条...这是我正在使用的东西:def startProgress(title):
global progress_x
sys.stdout.write(title + ": [" + "-"*40 + "]" + chr(8)*41)
sys.stdout.flush()
progress_x = 0
def progress(x):
global progress_x
x = int(x * 40 // 100)
sys.stdout.write("#" * (x - progress_x))
sys.stdout.flush()
progress_x = x
def endProgress():
sys.stdout.write("#" * (40 - progress_x) + "]\n")
sys.stdout.flush()
你调用startProgress传递操作的描述,然后progress(x)何地x是百分比,最后endProgress()