Python Download File Tutorial – How To Download File From Internet Using Python

Hello guys, in this tutorial i am going to show you how you can download file from internet. You will also learn to download different types of files such as PDF, HTML, ZIP etc. You will also learn to create a file downloader with a nice progress bar for your terminal using python. So let’s start Python Download File Tutorial.

One of the most important and common programming tasks to perform on the web is  downloading files from different online resources. A huge number of successful applications allow users to download files. So these are just a few web application functions that require downloading files:

  • File sharing
  • Retrieving website code (CSS, JS, etc)
  • Data mining
  • Social media

Python Download File – Most Popular Ways To Download Files Using Python

So guys there are many ways to download files using python.  Let’s see them one by one.

requests Module

Using requests module is one of the most popular way to download file. So first of all you need to install requests module, so run the following command on your terminal.

So now write the following code for downloading files using requests module.

  • Firstly import the requests module for performing HTTP request over the internet.
  • Then declare a url from where you want to download your file.
  • get( ) method of the requests module is used to download the file contents in binary format.
  • Now you have to open that filename in write binary(wb) mode.
  • output_file.write(r.content) means whatever the url contains, it will get all those contents and it will write all those to output_file.
  • Finally print Download Completed.

Now run this program and see what happens. Your file is successfully downloaded, now check this file in your ‘Download’ folder.

urllib.request Module

You can also use urllib.request module to download file over HTTP.

  • urlretrieve method of this module is used to download the file from internet.

Now write the following code.

Explanation

  • First of all, you have to install urllib.request module.
  • Then you have to create a variable that will contain the url of the file which you want to download.
  • Then call the urlretrieve( ) method. You have to pass two arguments to this method, the first one is url and the second one is the file path where you want to locate your file.
  • One thing should be keep in mind that you can pass any filename as the second parameter.

Now run the above code and check your download folder, you will see the file has been downloaded.

And now its time to move another section of this tutorial that is how to download different types of files such as text, html, pdf, image files etc using python.

Python Download File Tutorial –  Downloading PDF, HTML, Image And Text files

In this section, you will see how to download different types of file.

Downloading PDF File

Write the following code to download PDF file.

Now check your download location, you will see your file has been successfully downloaded.

Downloading HTML File

Write the following code for downloading an HTML file.

Let’s check your download location.

Downloading image File

Write the following code to download an image file.

Let’s Check A-Star Algorithm Python Tutorial – An Introduction To A* Algorithm In Python

Downloading Youtube Video File

  • For downloading youtube video, you have to install pytube module.
  • Run the following command on your terminal.

And now write the following code.

  • Now at first pass the url.
  • streams.first( ) method grabs the first format of the video.
  • download() method downloads the video.

Now run your code and you will see the video has been downloaded  to your passed location.

Downloading Text File

Write the following code for download a text file.

Now check the download location, you will see text file  has been downloaded.

Download Zip File

Write the following code to download zip file.

Now check the download location, you will see a zip file has been downloaded.

Also Read – Sets In Python Tutorial For Beginners

Python Download File – Downloading Large Files In Chunks, And With A Progress Bar

In this section, we will see how to download large files in chunks, download multiple files and download files with a progress bar. So let’s start.

Downloading Large Files In Chunks

You can also download large files in chunks. Its very easy, so let’s check it. Write the following program.

  • First of all import requests library.
  • Then specify url from where you want to download the file.
  • Now call the get( ) method and pass stream = True.
  • Now create a file whatever name you want to give it and open it in write binary mode.
  • Then you have to specify chunk size that you want to download at a time. I have set it 1025 bytes. Now you have to iterate through each chunk and write the chunks in the file until the chunks finished.
  • At last print a message Download Completed.

Now run the program, and check your download location, you will found a file has been downloaded.

Downloading File With Progress Bar

Now you will learn how can you download file with a progress bar. First of all you have to install tqdm module. Now run the following command on your terminal.

Now write the following code, i will explain it later.

  • First of all import the tqdm and requests module.
  • Then specify chunk size that is nothing but small amount of data that you will receive once at a time from the server of that url . Here i am taking chunk size 1024.
  • Then specify url from where you want to download your file.
  • Now you need to create a response object of request library. So you have to make a HTTP get request. Pass the url and set stream = True to the get( ) method.
  • Then define the total size of your file.
  • And now you need to create an output file and open it in write binary mode.
  • Now start a loop to get content of the response that you have made earlier. Then define tqdm( ) function and inside this define iterable which you are going to use. iterable is nothing but a sequence. And define the chunk size and total size and then unit.
  • Then you have to just write data like this file.write(data).
  • Finally just print Download Completed message.

Now run the code, you will see progress bar as below on your terminal.

Python Download File
Python Download File

This is very nice. You can see the file size is 2922KB and it only took 49 second to download the file.

Have You Checked – Python Zip File Example – Working With Zip Files In Python

So guys we have successfully completed this Python Download File Tutorial. I hope, you found it helpful if yes then must share with others. And if you have any query regarding this tutorial then feel free to comment. And yes for getting python tutorials stay tuned with Simplified Python. Thanks

2 thoughts on “Python Download File Tutorial – How To Download File From Internet Using Python”

Leave a Comment