How to Get File Size in Python: A Quick Guide

by | Python

Checking the size of a file is a common task when working with files and directories in Python. In this article, you’ll learn some popular methods for doing so.

Python offers several ways of finding file sizes. The standard os library has two functions, os.stat() and os.path.getsize(), that are commonly used to retrieve a file size in bytes. The pathlib library provides a more object-oriented approach, while the seek and tell methods let you get the details from file objects in memory.

This article dives deep into these methods and provides code examples to show you how to implement them in your Python projects.

Firstly, let’s look at the os Module.

How to Use the os Module to Get File Size

how to get file size in python

The Python os module provides various functions to interact with the operating system and work with files and directories. Two primary functions provide information about sizes:

  • os.path.getsize()
  • os.stat()

1. os.path.getsize() Function

The os.path.getsize() function is one of the simplest ways to get the file size in Python. The getsize function returns the file size in bytes directly. You provide the file path as the argument.

import os

# Replace 'your_file_path' with the actual file path
file_path = 'your_file_path'

try:
    file_size = os.path.getsize(file_path)
    print(f"File Size in Bytes is {file_size}")
except FileNotFoundError:
    print("File not found.")
except OSError:
    print("OS error occurred.")

The code above will output the size in bytes.

2. os.stat() Function

The os.stat() function is a part of the os library, and it returns several file attributes, including the size. In order to get the file size using the os.stat() function, you need to access the st_size attribute from the result.

Here’s an example:

import os

# Replace 'your_file_path' with the relative path
file_path = 'your_file_path'

try:
    file_stats = os.stat(file_path)
    file_size = file_stats.st_size
    print(f"File Size in Bytes is {file_size}")
except FileNotFoundError:
    print("File not found.")
except OSError:
    print("OS error occurred.")

In the example above, replace ‘your_file_path’ with the actual directory and file name.

Error Handling With Files in Python

illustration of error

When using these os functions, you should ensure that your code handles the two most common errors:

  • FileNotFoundError if the file does not exist.
  • OSError for other errors.

The “file not found” error is the most typical. It usually occurs when the file name or folder path specified in the script is incorrect. Check out our guide to the os.path.join method to learn the safest way to work with full paths.

The sample code in the prior sections uses Python’s exception-handling mechanism, the try and except block, to catch the errors with simple print statements. You can add more sophisticated solutions to your error scenarios.

How to Use the pathlib Module To Get File Sizes in Python

illustration of python code in an editor

The pathlib module is a part of Python’s standard library and provides an object-oriented way to work with file paths. You combine several functions and attributes to get the file size. Here are the steps:

  1. Create a file path object using the Path() method.
  2. Use the stat() function to retrieve the file attributes such as file size.
  3. Access the st_size attribute to retrieve the file size in bytes.

The below example puts this together:

from pathlib import Path

file_path = Path("path/to/your/file.txt")
file_stats = file_path.stat()

# python check file size
file_size_bytes = file_stats.st_size

print(f"The file size in bytes is: {file_size_bytes}")

If you’re running the sample code, replace the string with the absolute path of your file.

How to Use Seek and Tell to Check File Size in Python

illustration of file pointers

There are two common techniques to handle operations on file-like objects in Python: the seek() and tell() methods.

Both methods manipulate the file pointer or cursor. The file pointer represents the current position in the file from where the next read or write operation can take place.

To find the size of a file object, you can use these methods together in two steps:

  1. Move the cursor to the end of the file using the seek() method.
  2. Call the tell() method to get the current cursor location, which represents the total size in bytes.

The seek method takes two arguments:

  • offset (defaults to 0)
  • whence (2 is the end of the file)

Here is some sample code that shows the two methods in use on a file object:

def get_file_size(file_object):
    file_object.seek(0, 2) # Move the cursor to the end of the file
    size = file_object.tell() # Get the cursor position, which is the size of the file
    return size

file = open("example.txt", "rb")
print(get_file_size(file))

These methods are useful when you have already opened the file to read the data. Here’s a video that looks at loading datasets in Python.

File Size Conversion in Python

You’ll often need to convert file sizes from bytes to other units such as megabytes or gigabytes.

Once you have the file size in bytes using methods explained earlier, you can easily convert it to other units. To convert it to megabytes, divide the file size in bytes by (1024 * 1024):

file_size_megabytes = file_size_bytes / (1024 * 1024)

Similarly, to convert the file size to gigabytes, divide the file size in bytes by (1024 1024 1024):

file_size_gigabytes = file_size_bytes / (1024 * 1024 * 1024)

Final Thoughts

You’ve learned several different ways to get file sizes in python: using the os module, the pathlib module, or the seek and tell methods of the file object.

Choosing which method to use depends largely on your specific requirements and the nature of the file management tasks you’re handling. The simplest method is the os.path.getfilesize() function. However, if you have the file open in memory, you may find the seek and tell methods more appropriate.

Whether you’re building a file management application or loading data from a single file, knowing how to check the file size is a valuable part of your Python skill set. If you get stuck at any point, make use of our handy Python cheat sheet. It can be a valuable resource in your coding journey!

author avatar
Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

Related Posts