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.
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.
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:
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.
And that is, just few line of code and work is done!