Get the Size of a File in Bytes in Python

To get the size of a file in bytes in Python, you can use the os.path.getsize() function or the stat() method from the os module. These methods return the file size in bytes, helping you determine how much space a file occupies on disk.


Examples to Get the Size of a File

Consider the following example.txt file whose size we shall find in the following examples.

In highlighted in the screenshot, there are 37 bytes in the file.

1. Using os.path.getsize() to Get File Size

In this example, we will use the os.path.getsize() function to get the size of a file example.txt in bytes.

main.py

</>
Copy
import os

# Specify the file path
file_path = "example.txt"

# Get the file size in bytes
file_size = os.path.getsize(file_path)

# Print the file size
print(f"Size of '{file_path}': {file_size} bytes")

Explanation:

  1. We import the os module to access file-related functions.
  2. The variable file_path stores the name of the file for which we want to determine the size.
  3. os.path.getsize(file_path) retrieves the size of the file in bytes.
  4. We print the file size along with the file name.

Output:

Size of 'example.txt': 37 bytes

2. Using os.stat() to Get File Size

Here, we use the os.stat() function, which provides detailed information about a file, including its size in bytes.

main.py

</>
Copy
import os

# Specify the file path
file_path = "example.txt"

# Get the file size using os.stat()
file_size = os.stat(file_path).st_size

# Print the file size
print(f"Size of '{file_path}': {file_size} bytes")

Explanation:

  1. We import the os module.
  2. The variable file_path contains the file’s name.
  3. os.stat(file_path) retrieves file statistics, and st_size specifically provides the file size in bytes.
  4. We print the result, showing the file size.

Output:

Size of 'example.txt': 37 bytes

3. Using Path.stat().st_size from pathlib to Get File Size

We can also use the pathlib module, which provides an object-oriented approach to handling file paths in Python.

main.py

</>
Copy
from pathlib import Path

# Specify the file path
file_path = Path("example.txt")

# Get the file size using Path.stat()
file_size = file_path.stat().st_size

# Print the file size
print(f"Size of '{file_path}': {file_size} bytes")

Explanation:

  1. We import Path from the pathlib module.
  2. The variable file_path is created as a Path object.
  3. file_path.stat().st_size retrieves the file size in bytes.
  4. The result is printed, displaying the file size.

Output:

Size of 'example.txt': 37 bytes

4. Using open() and seek() to Get File Size

In this example, we open the file in binary mode and use seek() to determine its size.

main.py

</>
Copy
# Open file in binary read mode
with open("example.txt", "rb") as file:
    file.seek(0, 2)  # Move to the end of the file
    file_size = file.tell()  # Get the current position, which is the file size

# Print the file size
print(f"Size of 'example.txt': {file_size} bytes")

Explanation:

  1. We open the file in binary read mode ("rb"). Reference: Open file in binary mode.
  2. seek(0, 2) moves the file pointer to the end.
  3. tell() returns the current file position, which is the file size.
  4. We print the result, showing the file size in bytes.

Output:

Size of 'example.txt': 37 bytes

Conclusion

In summary, there are multiple ways to get the size of a file in Python:

  1. os.path.getsize(): A simple way to get file size.
  2. os.stat().st_size: Provides additional file information along with size.
  3. pathlib.Path().stat().st_size: An object-oriented approach.
  4. Using open() and seek(): A low-level approach that reads file position.

Each method is useful depending on your requirements, with os.path.getsize() being the simplest and pathlib being more Pythonic.