Get the size of a file from URL in Python

In this tutorial, we will learn how to get the size of a file from URL in python. Before getting on to the actual code, let us see some prerequisites for the same.

If you want to get the size of a file of your local storage you can follow this one – How to get the size of a file in Python

The urllib module

The urllib module is used to access and handle URL (Uniform Resource Locator) related data. Opening the URL, accessing, retrieving and downloading data, etc are some of the functions of urllib. In this tutorial, we will use urllib.request module to access file data. This module has predefined classes and functions which are needed for URL operations. One of the functions is urlopen(). As the name suggests, it opens the URL and fetches data. To access urllib.request, simply import it.

import urllib.request

Requests module

Another method to go by this problem is to use the requests module. It is one of the most famous, easy-to-use, third-party libraries in python and it is  used to make all kinds of HTTP/1.1 requests. To get started with this module, install it using:

pip install requests

And then import it in your code.

import requests

The head() method requests the URL to give access to the header details of the file. This is very useful when you only need the status and basic details of the file and not it’s contents.

Getting the size of a file from URL

Problem statement: Write a python program to get the size of a file from URL.

METHOD 1:

Steps/Algorithm:

  1. Import the urllib module.
  2. Paste the required URL.
  3. Get the size of the file using .length function.

Program/Code:

import urllib.request
#importing the module

file = urllib.request.urlopen("https://speed.hetzner.de/100MB.bin")
#just a dummy file

print(file.length)
#fetching its length

Output:

104857600

Python returns the size of the file in bytes.

METHOD 2:

Steps/Algorithm:

  1. Import the requests module.
  2. Paste the URL.
  3. Get the header details.
  4. Print it.

Program/Code:

import requests
#importing the requests module

url = "https://speed.hetzner.de/100MB.bin"
#just a dummy file URL

info = requests.head(url)
#fetching the header information

print(info.headers)
#printing the details

Output:

{'Server': 'nginx', 'Date': 'Tue, 23 Jul 2019 21:22:16 GMT', 'Content-Type': 'application/octet-stream', 'Content-Length': '104857600', 'Last-Modified': 'Tue, 08 Oct 2013 11:48:13 GMT', 'Connection': 'keep-alive', 'ETag': '"5253f0fd-6400000"', 'Strict-Transport-Security': 'max-age=15768000; includeSubDomains', 'Accept-Ranges': 'bytes'}

The ‘Content-Length’ gives the size of the file in bytes.

 

Leave a Reply

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