0
for url in addresses:
    file_name = url.rsplit('/', 1)[-1]
    fname_with_path = os.path.join(download_directory, file_name)


    attempts = 1
    while attempts < 5:
        try:
            urllib.request.urlretrieve(url, fname_with_path)
            print("%-3s %-60s %25s" % ('--', file_name, 'downloaded'), end='')
            break
        except:
            attempts += 1
            if attempts > 1 and attempts < 5:
                print('tried to download   ', file_name, '   attempt:', attempts)
            if attempts == 5:
                print("%-3s %-60s %25s" % ('--', file_name, 'FAILED'), end='')
            pass

Here's a part of code for downloading files from a list of urls. Since some files are big enough I'd like to know many KBs of a file are alredy downloaded. For example

file1: 348 / 2980

Also I'd like to update the progress on the same line, not like this:

file1: 348 / 2980
file1: 355 / 2980
file1: 389 / 2980
file1: 402 / 2980
sovel2
  • 405
  • 4
  • 11

3 Answers3

1

You can print out a carriage return (\r) to overwrite previous printout: See also: How do I write output in same place on the console?

leonardseymore
  • 533
  • 5
  • 20
0

This is how you can print onto the same line in python 3:

print('text\r', end='', flush=True)

The \r is a carriage return and moves the cursor back to the start of the line

Keir
  • 752
  • 5
  • 8
0

I do know how it does in c/c++ languages. Just put \r at beginning of the line you want to stay on the same place and remove \n at the end.

https://github.com/python/cpython/blob/3.7/Lib/urllib/request.py#L283

In your case seems the urlretrieve uses reporthook(blocknum, bs, size) where you can override the report code as you wish, for example, like i said above.

Andry
  • 2,273
  • 29
  • 28