How to Download Files in Python?

By | February 21, 2021

Generally, we use the web browser to download a file from the internet. The downloading of a file is similar to accessing a web-page from the server. We sent the get request to the file url address and in response, we get the file data.

In this Python tutorial, I will walk you through the Python program to download files from the internet, using a simple HTTP request.

Vamware

In this tutorial, I will be downloading the Python latest 3.9 .exe file from the Internet using the Python program. But before we dive into the code let’s install the libraries we will be using in this tutorial.

Install Libraries

Python requests library

requests is one of the most popular Python libraries that is used to send HTTP requests. Mostly all the Python web frameworks use this library for HTTP requests. And in this tutorial, we will be using this library to send GET requests to the file url that we supposed to download.

requests is not apart of Python Standard libraries so we need to install it for our Python environment using the pip install command.

pip install requests

Python progress library (optional)

The second library that we will be using in this Program is progress, which is an open-source third-party Python library. We will be using this library to display a console-based progress bar for the downloading progress of our file.

To download the progress library for your Python environment run the following pip command on your terminal or command prompt.

pip install progress

How to Download Files in Python?

Alright, now let’s get started with the Python program to download a file from the internet.

Let’s start with importing the modules.

import requests
from progress.bar import Bar

After importing the modules let’s define the file_url string variable that represents the url for the file that we supposed to download.

In this tutorial, I will download the latest version of Python from its official website, so I require the file url path. To get the downloading file url you can right-click on the download button link and copy the link address.

file_url=”https://www.python.org/ftp/python/3.9.1/python-3.9.1-amd64.exe”

after defining the file url, let’s extract the file name from the url and send a get request to the file url.
filename = file_url.split("/")[-1]

#send get request
response = requests.get(file_url, stream=True)

The requests get(file_url, stream=True) function will send a get request to the  file_urland the stream=True attribute will make sure that the response does not download the all the file at once instead it download the file in the data chunks.

After sending the get request to the file url, you will receive the file response in chunks that you can iterate over and download the complete file. Before we start downloading the file data in our local system let’s get the total size of the response file.

file_size = int(response.headers.get("Content-Length", 0))

the headers.get("Content-Length") will return the total length of data which represents the total size of data in bytes.

Now let’s get every chunk of data from the response object using iter_content() function.

with Bar(f'Downloading {filename}', fill='*',suffix='%(percent)d%%') as bar:
    #write file in binary mode
    with open(filename,"wb") as file:
        #iterate over the response in data chunks
        for data in response.iter_content(chunk_size=file_size//100):
            file.write(data)   
            bar.next()  #increase downloading bar 
            
print("File has been downloaded successfully")

Bar(f'Downloading {filename}', fill='*',suffix='%(percent)d%%') as bar statement is for the downloading bar process.

with open(filename,"wb") as file: statement will create a filename and write data in binary mode.

for data in response.iter_content(chunk_size=file_size//100): statement will iterate over the response, and get the specified number of data chuck in every iteration.

With each iteration, we will get file_size//100 data values.

Now put all the code together and execute.

Python program to download files from the internet

from progress.bar import Bar
import requests    #pip install requests

file_url="https://www.python.org/ftp/python/3.9.1/python-3.9.1-amd64.exe"

filename = file_url.split("/")[-1]

#send get request
response = requests.get(file_url, stream=True)

file_size = int(response.headers.get("Content-Length", 0))

with Bar(f'Downloading {filename}', fill='*',suffix='%(percent)d%%') as bar:
    #write file in binary mode
    with open(filename,"wb") as file:
        #iterate over the response in data chunks
        for data in response.iter_content(chunk_size=file_size//100):
            file.write(data)   
            bar.next()  #increase downloading bar 
            
print("File has been downloaded successfully")

Output

When you execute the above code the downloading process will begin. After the process gets finished you can check your directory there you will see the downloaded file.

Conclusion

In this Python tutorial, you learned “How can you download files from the Internet using Python”. In the above program I have used two third party libraries requests and progress. Using the requests library I sent a GET request to the file url and with the progress library, I showed the downloading progress bar which is nothing but just the 100 write data lines with 100 iterations.

If you want to learn how to download all the Images from a web-page using Python, you can click here.

Leave a Reply

Your email address will not be published. Required fields are marked *