There are several different ways of accessing the size of a file in Python, there are ways of doing this through operating system calls through your file system, and there are ways of retrieving a file object's size efficiently by reading the file into Python. The method chosen will depend on your specific use-case.
Get size of a static file from the operating system
The most straight forward way to retrieve a file's size is by using either the os.stat() method or the os.path.getsize() method.
For this tutorial we will use the following simple text file:
myfile.txt
Hello world!
The os.path.getsize() method will just return the size of the file on your system in bytes, it requires only the relative or absolute file path as an argument:
import os
# Get the file size from the os.path.getsize() method and print it out
file_size = os.path.getsize('myfile.txt')
print(f"The file size is {file_size} bytes")
This will give the following output:
The file size is 13 bytes
The os.stat() method will return an object containing several filesystem descriptors of the file including the file's size, last modified time, user identifier, etc., it is useful to use this method if you wish to access more information about a file. As with the os.path.getsize() method, os.stat() requires just the file path as an argument
import os
# Get the file size from the os.stat() method and print it out
file_stat = os.stat('myfile.txt')
print(f"The file size is {file_stat.st_size} bytes")
This will give the following output:
The file size is 13 bytes
The pathlib module also provides a similar method to os.stat() to get a file's size in Python, this is useful for when you're primarily using the pathlib module for other reasons and want to keep things organised. The stat() method of a pathlib.Path object returns the same os.stat_result object as the os.stat() method, containing the file's size and last modified times.
import pathlib
# Create a Path object from your file path
my_file = pathlib.Path('myfile.txt')
# Get the file size from the pathlib.Path.stat() method and print it out
file_stat = my_file.stat()
print(f"The file size is {file_stat.st_size} bytes")
This will give the following output:
The file size is 13 bytes
Get the size of an open file object in Python
A more reliable way of determining a file's size in Python is by opening it as a file object, seeking to the end of the file and accessing the cursor's position at the end of the file which will be equal to the file's size in bytes.
This method works well for determining the size of a large file object in Python in real time.
Once you've opened the file the first step is to seek to the end of the file, this can be done using the file.seek() method which accepts the position in the file as well where it is relative to as arguments, our arguments we would want to set as position 0, relative to the end of the file, which is denoted by either 2 or more reliably os.SEEK_END as some operating systems could support other values.
Next we can use the file.tell() method to retrieve the file cursor's current position in the file which will be equavalent to the file's size in bytes.
import os
# Open the file
with open('myfile.txt', 'rb') as infile:
# Seek position 0 relative to end of file (os.SEEK_END)
# Second argument can be 2, but some OSs could support other values
infile.seek(0, os.SEEK_END)
# Use file.tell() to retrieve the cursor's current position (which is the end of the file)
file_size = infile.tell()
print(f"The file size is {file_size} bytes")
This will give the following output:
The file size is 13 bytes
Note: This method will work when opening a file in non-binary mode ('r') but may not give an accurate reading if the file contains strange encodings. For example you will likely need to open an image file in binary mode to get a proper reading of the file size.
if you have any questions or would like to add anything to this tutorial feel free to leave a comment below.