Skip to content Skip to sidebar Skip to footer

Python 3: How To Create A Text Progress Bar For Downloading Files?

I have currently this: def download_dropbox(url, pre_file_name): file = url[42:] file = file[:-5] file_name = pre_file_name + file print('Downloading from ' + url +

Solution 1:

To answer your main question, how to make a text progress bar, you could use something like the following to give you an idea:

import time

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

This would give you the following:

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

Your main problem though is that there is no obvious way to determine how many bytes will eventually be downloaded unless you already know the exact size of the item being downloaded beforehand. If you control the server end then you could arrange for the length to be obtained before starting.

You can though start by at least converting your read() line to something like the following:

u = urllib.request.urlopen(url)

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

    ifnotlen(block):
        break

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

By doing it this way, you read it a bit at a time and can then provide feedback.

Solution 2:

You can use print with \r at the start to go to the start of the line and write over the previous text (so you need to write spaces if you want to clear a character). Here's a simple example:

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

Post a Comment for "Python 3: How To Create A Text Progress Bar For Downloading Files?"