How To Download File Using Python Over HTTP
Python is one of the top programming languages and easy to learn for beginners. In previous tutorial we have learned how to install Python in Windows system, Create Twitter Bot using Python and Tweepy. Here is another tutorial in Python serial. In this article we will learn how to download file from Url using python 3.
Read More: How To Install Python in Windows
As we are using Python 3 here, you can also use other python version such as python 2.7 as well.
How To Download File using python Script
Start from importing the python modules to download the file and updating download status to user.
1
2
3
4
|
# How To Download File Using Python Over HTTP
# Created By Gopal Joshi
from urllib.request import urlopen
|
import
will import the python libraries such as urlopen libraries. urlopen module is used to access file from URL.
1
2
3
4
5
6
7
8
|
url = "http://localhost/TestFile.zip" # Specify Desired Url from
file_name = url.split('/')[-1]
u = urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
totalFileSize = int(meta['Content-Length']) # Calculating File Size
print ("Downloading: %s" % (file_name))
|
Below code will download file and show download status to user.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
bufferSize = 9192
count = fielSize = 0
while True:
buffer = u.read(bufferSize)
if not buffer:
break
fielSize += len(buffer)
f.write(buffer)
per = (fielSize * 100 / totalFileSize)
status = "[%3.2f%%]" % per + "." * (int)(per/2)
if count > 200:
count = 0
print (status)
else:
print (status, end="\r")
count += 1
f.close()
|
python print is used to display the output to a user. It will calculate how much file is downloaded and how much is remaining to download using buffer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import time
from urllib.request import urlopen
url = "http://localhost/TestFile.zip" # Specify Desired Url from
file_name = url.split('/')[-1]
u = urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
totalFileSize = int(meta['Content-Length']) # Calculating File Size
print ("Downloading: %s" % (file_name))
bufferSize = 9192
count = fielSize = 0
while True:
buffer = u.read(bufferSize)
if not buffer:
break
fielSize += len(buffer)
f.write(buffer)
per = (fielSize * 100 / totalFileSize)
status = "[%3.2f%%]" % per + "." * (int)(per/2)
if count > 200:
count = 0
print (status)
else:
print (status, end="\r")
count += 1
f.close()
|
Save the file as downloadFile.py
in file directory (Eg. D:/downloadFile.py for windows). Here .py
is file extension for python scripts
To Execute the file, Open Command Prompt or Terminal in case you are using Linux operating system and run below command.
1
|
$ py downloadFile.py
|
Above Image: Show the output of execution
Read More:
Create Twitter Bot Using Python and Tweepy
sSwitch – JQuery Plugin For Sliding Toggle Switches
How To Create Simple JQuery plugin
How To Implement Infinite Scroll Using JQuery, Ajax and PHP
Integrate Google Prettify in Html Page
Verify Your Server Meets PayPal Payment Gateway Requirements