Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python progress bar and downloads

I have a Python script that launches a URL that is a downloadable file. Is there some way to have Python display the download progress as oppose to launching the browser?

like image 547
user1607549 Avatar asked Mar 26 '13 18:03

user1607549


People also ask

How do you code a progress bar in Python?

In order to create the progress bar in Python, simply add the highlighted syntax into the code: from tqdm import tqdm. tqdm(my_list)

What is tqdm () in Python?

tqdm is a library in Python which is used for creating Progress Meters or Progress Bars. tqdm got its name from the Arabic name taqaddum which means 'progress'. Implementing tqdm can be done effortlessly in our loops, functions or even Pandas.

What is download function in Python?

To download a file from a URL using Python, use the requests.get() method. For example, let's download Instagram's icon: import requests. URL = "https://instagram.com/favicon.ico"


1 Answers

I've just written a super simple (slightly hacky) approach to this for scraping PDFs off a certain site. Note, it only works correctly on Unix based systems (Linux, mac os) as PowerShell does not handle "\r":

import sys import requests  link = "http://indy/abcde1245" file_name = "download.data" with open(file_name, "wb") as f:     print("Downloading %s" % file_name)     response = requests.get(link, stream=True)     total_length = response.headers.get('content-length')      if total_length is None: # no content length header         f.write(response.content)     else:         dl = 0         total_length = int(total_length)         for data in response.iter_content(chunk_size=4096):             dl += len(data)             f.write(data)             done = int(50 * dl / total_length)             sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )                 sys.stdout.flush() 

It uses the requests library so you'll need to install that. This outputs something like the following into your console:

>Downloading download.data

>[=============                            ]

The progress bar is 52 characters wide in the script (2 characters are simply the [] so 50 characters of progress). Each = represents 2% of the download.

like image 156
Endophage Avatar answered Sep 20 '22 14:09

Endophage