Python3:如何制作下载文件的文字进度条?

Python 3: How to create a text progress bar for downloading files?

我目前有这个:

def download_dropbox(url, pre_file_name):
    file = url[42:]
    file = file[:-5]
    file_name = pre_file_name + file
    print('Downloading from ' + url + ' to ' + file_name)
    print(file)
    u = urllib.request.urlopen(url)
    data = u.read()
    u.close()

    with open(file_name, "wb") as f:
        f.write(data)
    print('Download Completed from ' + url + ' and saved to ' + file_name)

这基本上是从保管箱下载文件并将其保存到目录中。但是我希望能够有某种文本进度条,例如:


[====]50%



50%

我认为最困难的部分是使用加载栏模块等任何外部模块来完成它。此外,如标题所述,我需要它 python 3. Thank-you。

编辑:

感谢 Martin Evans 提供 while 循环和进度条读取的数据,这里是代码的最终结果:

#Get the total number of bytes of the file to download before downloading
print ("opening url:", url)
u = urllib.request.urlopen(url)
meta = u.info()
print(str(meta).split())
metaInfo = str(meta).split()
print(len(metaInfo))
print ("Content-Length:" + metaInfo[46] + " bytes")
fileTotalbytes=int(metaInfo[46])

data_blocks = []
# total = int(metaInfo[46])
total=0

while True:
    block = u.read(1024)
    data_blocks.append(block)
    total += len(block)
    hash = ((60*total)//fileTotalbytes)
    print("[{}{}] {}%".format('#' * hash, ' ' * (60-hash), int(total/fileTotalbytes*100)), end="\r")

    if not len(block):
        break

data=b''.join(data_blocks) #had to add b because I was joining bytes not strings
u.close()

with open('test.zip', "wb") as f:
        f.write(data)

您可以使用 print with \r 在开始时转到该行的开头并覆盖之前的文本(因此如果您想清除一个字符,则需要写空格)。这是一个简单的例子:

from time import sleep
x = 0
while x < 20:
    print('\r' + '.' * x, end="")
    x += 1
    sleep(0.1)

要回答您的主要问题,即如何制作文本进度条,您可以使用类似以下的内容来给您一个想法:

import time

for n in range(1,101):
    hash = ((60*n)//100)
    print("[{}{}] {}%".format('#' * hash, ' ' * (60-hash), n), end="\r")
    time.sleep(0.05)

这会给你以下内容:

[###########################                                 ] 45%

你的主要问题是没有明显的方法来确定最终将下载多少字节,除非你事先知道正在下载的项目的确切大小。如果您控制服务器端,那么您可以安排在开始之前获取长度。

不过,您至少可以先将 read() 行转换为如下内容:

u = urllib.request.urlopen(url)

data_blocks = []
total = 0

while True:
    block = fd.read(1024)
    data_blocks.append(block)
    total += len(block)
    print("Downloaded {} bytes".format(total), end="\r")

    if not len(block):
        break

data = "".join(data_blocks)
u.close()

通过这种方式,您可以一次阅读一点,然后可以提供反馈。