How to Get the File Size in MB using Python?

As a software developer working on a project for a client in the USA, I recently faced a scenario where I needed to retrieve the size of a file in MB. After researching and experimenting with different methods, I discovered several effective ways to accomplish this task. In this tutorial, I will explain how to get the file size in MB using Python with examples.

Get the File Size in MB using Python

Before getting into the code, let’s briefly discuss the different units used to measure file sizes:

  • Byte (B): The fundamental unit of digital information.
  • Kilobyte (KB): 1 KB = 1,024 bytes.
  • Megabyte (MB): 1 MB = 1,024 kilobytes.
  • Gigabyte (GB): 1 GB = 1,024 megabytes.

In this tutorial, we will focus on obtaining the file size in megabytes (MB).

Read How to Check If a File Exists and Create It If Not in Python?

Method 1: Use the os.path.getsize() Function

The os module in Python provides the getsize() function, which allows us to retrieve the size of a file in bytes. To get the file size in MB, we need to convert the byte value to megabytes.

Here’s an example of how to use the os.path.getsize() function to get the file size in MB:

import os

file_path = "/path/to/your/file.txt"
file_size_bytes = os.path.getsize(file_path)
file_size_mb = file_size_bytes / (1024 * 1024)

print(f"File size: {file_size_mb:.2f} MB")

Output:

File size: 0.00 MB

You can refer to the screenshot below to see the output.

Get the File Size in MB using Python

In this example, we provide the file_path variable with the path to the file we want to measure. The os.path.getsize() function returns the file size in bytes, which we store in the file_size_bytes variable. To convert bytes to megabytes, we divide the byte value by (1024 * 1024). Finally, we print the file size in MB using an f-string with .2f to display the result with two decimal places.

Check out How to Copy File and Rename in Python

Method 2: Use the os.stat() Function

Another approach to get the file size in MB is by using the os.stat() function. This function returns a stat_result object that contains various file attributes, including the file size in bytes.

Here’s an example of how to use the os.stat() function to get the file size in MB:

import os

file_path = "/path/to/your/file.txt"
file_stats = os.stat(file_path)
file_size_bytes = file_stats.st_size
file_size_mb = file_size_bytes / (1024 * 1024)

print(f"File size: {file_size_mb:.2f} MB")

Output:

File size: 0.27 MB

You can refer to the screenshot below to see the output.

How to Get the File Size in MB using Python

In this example, we pass the file_path to the os.stat() function, which returns a stat_result object. We access the st_size attribute of this object to get the file size in bytes. Similar to the previous method, we divide the byte value by (1024 * 1024) to convert it to megabytes and print the result.

Read How to Import a Class from a File in Python

Method 3: Use a Custom Function

To make the process of getting the file size in MB more convenient and reusable, we can create a custom function that encapsulates the logic.

Here’s an example of a custom function to get the file size in MB:

import os

def get_file_size_in_mb(file_path):
    file_size_bytes = os.path.getsize(file_path)
    file_size_mb = file_size_bytes / (1024 * 1024)
    return file_size_mb

file_path = "/path/to/your/file.txt"
file_size_mb = get_file_size_in_mb(file_path)

print(f"File size: {file_size_mb:.2f} MB")

Output:

File size: 1.07 MB

You can refer to the screenshot below to see the output.

Get the File Size in MB using Python Custom Function

In this example, we define a function called get_file_size_in_mb() that takes the file_path as a parameter. Inside the function, we use the os.path.getsize() function to get the file size in bytes and then convert it to megabytes. The function returns the file size in MB, which we can then use in our code.

Check out Python file Does Not Exist Exception

Example

Let’s consider a real-world scenario where getting the file size in MB can be useful. Suppose you are working on a project for a media company based in New York City. The company deals with large video files and needs a way to monitor the file sizes to ensure efficient storage and transfer.

Here’s an example of how you can use Python to get the file size in MB for a video file:

import os

def get_file_size_in_mb(file_path):
    file_size_bytes = os.path.getsize(file_path)
    file_size_mb = file_size_bytes / (1024 * 1024)
    return file_size_mb

video_file_path = "/path/to/video/nyc_skyline.mp4"
video_file_size_mb = get_file_size_in_mb(video_file_path)

print(f"Video file size: {video_file_size_mb:.2f} MB")

In this example, we have a video file named “nyc_skyline.mp4” located at the specified video_file_path. By using the get_file_size_in_mb() function, we can easily retrieve the size of the video file in megabytes. This information can be valuable for the media company to optimize their storage and ensure smooth file transfers.

Read Python File methods

Conclusion

In this tutorial, I explained how to get the file size in MB using Python. I discussed three methods to accomplish this task, they use the os.path.getsize() function, use os.stat() function, use a custom function. I also discussed real-world examples.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.