3 Methods for Using Python Download file from the Internet

Downloading any file from the internet is just a question of few clicks, but what about coding this thing! Yes, in this tutorial i will teach you how you 3 ways how you can download any file from the internet using Python script.

So if you’re a beginner we highly recommend that you also check out our series on Learning Python programming by killing Zombies, Alternatively you can also look for many other Python tutorials here like how to make your own Youtube video Downloader using Python and build your own Python Snake Game.

In this tutorial we will try downloading this random file from the internet: http://speedtest.ftp.otenet.gr/files/test10Mb.db

3 Methods for Using Python Download file from the Internet

Method 1: Using wget | Python Download file method

Using wget method is the simplest most and the perfect method that i like to download my files from the internet. Wget is used for retrieving files using HTTP, HTTPS, FTP and FTPS connections which are already the most widely-used Internet protocols.

Like what you're reading? Subscribe to our Weekly Tech Newsletter!

* indicates required
Using wget Method
fileUrl = 'http://speedtest.ftp.otenet.gr/files/test10Mb.db'
import wget
file_name = wget.download(fileUrl)
100% [........................................................................] 10485760 / 10485760

The file is saved at the location of the script.

Method 2: Using urllib | Python Download file method

Urllib provides a high level of interface for downloading files from the internet. It accepts URL to download any file as shown in the code below. Urllib performs all the necessary certificate and hostname checks by default but same is not he case if you are using Python version below 2.7.9.

Using urllib, we can download any file from the internet using following code:

Using wget Method
import urllib
fileUrl = 'http://speedtest.ftp.otenet.gr/files/test10Mb.db'
urllib.request.urlretrieve(fileUrl, "file10mb.db")


('file10mb.db', <http.client.HTTPMessage at 0x236f7331320>)

And voila, you file is successfully downloaded!

Method 3: Using requests | Python Download file method

If you are planning to use request then its highly recommended to use Python 3.

3 Methods for Using Python Download file from the Internet

Using Requests
file = requests.get(fileUrl)

with open('requestfile10mb.db', 'wb') as f:  

And that is, just few line of code and work is done!