文本控制台中的进度栏 [英] Text Progress Bar in the Console
本文介绍了文本控制台中的进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有很好的方法来做以下?
Is there a good way to do the following?
我写了一个简单的控制台应用程序,使用ftplib从FTP服务器上传和下载文件。
I wrote a simple console app to upload and download files from an FTP server using the ftplib.
每次下载一些数据块时,我想更新一个文本进度条,即使它只是一个数字。
Each time some data chunks are downloaded, I want to update a text progress bar, even if it's just a number.
但我不想删除所有打印到控制台的文本。 (执行清除,然后打印更新的百分比。)
But I don't want to erase all the text that's been printed to the console. (Doing a "clear" and then printing the updated percentage.)
推荐答案
我一直在使用@Vladimir下面的Ignatyev的解决方案和爱它:)
I've been using a modified version of @Vladimir Ignatyev's solution below and loving it : )
import sys
# Print iterations progress
def printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
barLength - Optional : character length of bar (Int)
"""
formatStr = "{0:." + str(decimals) + "f}"
percent = formatStr.format(100 * (iteration / float(total)))
filledLength = int(round(barLength * iteration / float(total)))
bar = '█' * filledLength + '-' * (barLength - filledLength)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
#
# Sample Usage
#
from time import sleep
# make a list
items = list(range(0, 100))
i = 0
l = len(items)
# Initial call to print 0% progress
printProgress(i, l, prefix = 'Progress:', suffix = 'Complete', barLength = 50)
for item in items:
# Do stuff...
sleep(0.1)
# Update Progress Bar
i += 1
printProgress(i, l, prefix = 'Progress:', suffix = 'Complete', barLength = 50)
# Sample Output
Progress: |█████████████████████████████████████████████-----| 90.0% Complete
这篇关于文本控制台中的进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文