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
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:
- We import the
os
module to access file-related functions. - The variable
file_path
stores the name of the file for which we want to determine the size. os.path.getsize(file_path)
retrieves the size of the file in bytes.- 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
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:
- We import the
os
module. - The variable
file_path
contains the file’s name. os.stat(file_path)
retrieves file statistics, andst_size
specifically provides the file size in bytes.- 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
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:
- We import
Path
from thepathlib
module. - The variable
file_path
is created as aPath
object. file_path.stat().st_size
retrieves the file size in bytes.- 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
# 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:
- We open the file in binary read mode (
"rb"
). Reference: Open file in binary mode. seek(0, 2)
moves the file pointer to the end.tell()
returns the current file position, which is the file size.- 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:
os.path.getsize()
: A simple way to get file size.os.stat().st_size
: Provides additional file information along with size.pathlib.Path().stat().st_size
: An object-oriented approach.- Using
open()
andseek()
: 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.