Python download file

Table of contents:

To download a file in Python you can do many things. Here are some options:

Using wget to download file

You will need to install wget first: pip install wget since wget is not in standard library.


import wget
url = ''
# in case we just like to use the default file name, out='') # will use as output file name

Where signature is:

def download(url, out=None, bar=bar_adaptive):
    """High level function, which downloads URL into tmp file in current
    directory and then renames it to filename autodetected from either URL
    or HTTP headers.

    :param bar: function to track download progress (visualize etc.)
    :param out: output filename or directory
    :return:    filename where URL is downloaded to

Using requests to download file


import requests
url = ''
response = requests.get(url, allow_redirects=True) # nostream
with open('', 'wb') as handle:

Download file with python progress bar


from tqdm import tqdm
import requests
url = ''
response = requests.get(url, stream=True)

with open('', 'wb') as handle:
    for data in tqdm(response.iter_content()):

Here is requests.get signature:

def get(url, params=None, **kwargs):
    r"""Sends a GET request.

    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the body of the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response

Unzip a file after download

Here is how to unzip file after download (in case of zip file):


import zipfile
with zipfile.ZipFile('', 'r') as h:

tags: python download file - download file - python wget - pip install wget - python wget downlad - python download file - python requests - python progress bar - progress bar python - unzip a file & category: python