问题描述
此代码演示进度并在同一控制台行打印 Done...
并退出程序
import time
filename = 'file-name.txt'
for i in range(0,101):
time.sleep(0.1)
print('Downloading File %s [%d%%]\r'% (filename,i),end="",flush=True)
print('Done...\r\n',flush=True)
问题是当程序结束时,它在最后一行的末尾有上一行的残余。下面的输出演示了程序运行时单行的更新。
电流输出(单行更新)
Downloading File file-name.txt [1%]
.
.
Downloading File file-name.txt [100%]
Done...ding File file-name.txt [100%]
预期输出(单行更新)
Downloading File file-name.txt [1%]
.
.
Downloading File file-name.txt [100%]
Done...
在 here 中发现了同样的问题,但它没有解决这个问题。
如何在没有前一行的残余的情况下实现预期输出。
我想添加 '\b\b\b\b\b\b\b\b\b\b'
不是很 Pythonic
解决方法
我复制了您提供的代码并在我的系统上运行了它。 输出符合您的预期。
```Downloading File file-name.txt [0%]
Downloading File file-name.txt [1%]
Downloading File file-name.txt [2%]
Downloading File file-name.txt [3%]
Downloading File file-name.txt [4%]
Downloading File file-name.txt [5%]
Downloading File file-name.txt [6%]
Downloading File file-name.txt [7%]
Downloading File file-name.txt [8%]
Downloading File file-name.txt [9%]
Downloading File file-name.txt [10%]
Downloading File file-name.txt [11%]
Downloading File file-name.txt [12%]
Downloading File file-name.txt [13%]
Downloading File file-name.txt [100%]
Done... ```