How to check the file size?


Files are used to store certain data based on user requirement. A file can contain any type of information suchas audio, text, images, video or program. Depending on the data present in a file, the size of a file varies.

The size of a file is measured in bits or bytes. A bit is the smallest unit of information which is typically represented by a single value: either 0 or 1. Bytes are used to measure the amount of data in a file and the bytes are denoted by 8 bits. There are several ways to check the file size in python. Let’s see them one by one.

Using os.path module

The os.path module in python, provides a function named getsize(), which is used to find the size of the file.

Syntax

Following is the syntax for finding the size of a file using getsize() method from os.path

from os import path
path.getsize(file_name)

Where,

  • Os is the module in python.

  • path is the method in os module.

  • getsize is the function used to calculate the file size.

  • file_name is the input file.

Example

In this example, we will pass a file to the os.path.getsize() method then the file size will be returned in bytes.

from os import path
file_location = "sample.py"
file_size = path.getsize(file_location)
print("File size in bytes:", file_size)

Output

Following is the output produced after executing the program above –

File size in bytes: 34

Using os.stat() Method

In python, os.stat() method accepts a file path as an argument and returns a list object consisting various attributes describing its structure. One of these attributes is the st_size, which contains the size of the file.

Example

In the following example we are retrieving the size of a file using st_size attribute –

import os
file_path = "sample.py"
file_stats = os.stat(file_path)
file_size = file_stats.st_size
print("File size in bytes:", file_size)

Output

The below is the output of the file size calculated using the os.stat() module.

File size in bytes: 34

Using pathlib Module

Python also provides another module pathlib, that provides the same function stat().st_size to calculate the size of a file.

from pathlib import Path
file_path = Path("articles.ipynb")
file_size = file_path.stat().st_size
print("File size in bytes:", file_size)

Output

The below is the output of the above code returned the size of the file.

Using os.fstat() Method

Using the os.fstat() method in python, we can find the size of a file.

The fstat() method is used to retrieve the status of a file descriptor. It accepts a file descriptor as an argument and returns a list object consisting attributes to describe the status of a file corresponding to the given file descriptor. You can retrieve the file size from these attributes using st_size.

from os import fstat
file_path = "articles.ipynb"
with open(file_path, "rb") as f:
    file_stats = fstat(f.fileno())
    file_size = file_stats.st_size
print("File size in bytes:", file_size)

Output

The following output displays the calculated size of a file –

File size in bytes: 16224

Note − If we want to find the size of the file, we can use above mentioned any one of the ways and we have pass the entire file path where the file is located as the input argument to the function.

Updated on: 09-Aug-2023

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements