我是python的新手,我正在编写一些脚本来自动从FTP服务器等下载文件.我想显示下载的进度,但我希望它保持在相同的位置,例如:
输出:
正在下载文件FooFile.txt [47%]
我试图避免这样的事情:
Downloading File FooFile.txt [47%] Downloading File FooFile.txt [48%] Downloading File FooFile.txt [49%]
我应该怎么做呢?
重复: 如何在命令行应用程序中打印当前行?
您还可以使用回车:
sys.stdout.write("Download progress: %d%% \r" % (progress) ) sys.stdout.flush()
使用像curses模块这样的终端处理库:
curses模块提供了curses库的接口,这是便携式高级终端处理的事实标准.
我喜欢以下内容:
print 'Downloading File FooFile.txt [%d%%]\r'%i,
演示:
import time for i in range(100): time.sleep(0.1) print 'Downloading File FooFile.txt [%d%%]\r'%i,Python 3
print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
演示:
import time for i in range(100): time.sleep(0.1) print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
\b
多次打印退格符,然后用新号码覆盖旧号码.
#kinda like the one above but better :P from __future__ import print_function from time import sleep for i in range(101): str1="Downloading File FooFile.txt [{}%]".format(i) back="\b"*len(str1) print(str1, end="") sleep(0.1) print(back, end="")
对于Python 3xx:
import time for i in range(10): time.sleep(0.2) print ("\r Loading... {}".format(i)+str(i), end="")