How to Easily Get a File Size in Python

Ever need to get the size of a file? These two methods show how Python makes easy work of this task without straying from its standard library!
python get file size alpharithms

The Python standard library offers developers an easy toolkit for getting associated file data. Getting the size of a file in Python is easily done via the OS module’s stat method. Here we’ll take a quick look at this method, how it’s used, and what one needs to be aware of.

Creating a Sample Text File

To get started with the os.stat() method, let’s create a sample file to work within the current working directory. This way, we’ll be on the same page as we work through getting the size of a file using the os module. The following code will create a text file named sample.txt with the word alpharithms written 256 times separated by the \n newline character:

# Create a sample file in the local working directory
with open('sample.txt', 'w')as file:
    file.write("alpharithms\n" * 256)

# Results in sample.txt
alpharithms
alpharithms
alpharithms
...
alpharithms <------ 256th line

Python makes getting the size of a file incredibly easy. There are two primary ways to achieve this with standard library methods—both of which use the os module. The first method relies on the os.path module and the second on the os.stat module. The stat module provides more detailed information but the os.path.getsize() method is more concise. Let’s take a look at how to get file size in Python using both.

os.path.getsize()

The getsize() method takes a path-like object (string, in the example below) as an argument. This method will return an integer value representing the size of the file. We can implement this as shown in the code below:

# Get the size of the file 
size = os.path.getsize("sample.txt")

>>> size

3328

os.stat()

The os.stat module provides a st_size attribute that informs the developer of the size of a file, again in bytes. This is achieved, referencing the previously-created sample file, in the following single line of code:

# Get the size of the file
size = os.stat("sample.txt").st_size

>>> size

3328

Converting Bytes into Kilobytes & Megabytes

So now we know that our file is of size 3328—but what exactly does that tell us? It certainly isn’t 3328 characters—our file had only 3072. The .st_size attribute of an os.stat() object returns the file size in bytes.

Checking a file’s size in bytes is useful in some cases. Other times, units like kilobytes (kb) or megabytes (MB) are preferred. Fortunately, knowing that 1024 bytes make a kilobyte and that 1024 kilobytes make a megabyte, we can do some easy conversions:

# Get the size of the file
size = os.stat("sample.txt").st_size

# Convert to familiar units
kb = size / 1024
mb = size / 1024 / 1024

# print the results to the console
print("size in kb:", kb)
print("size in mb:", MB)

# results
size in kb: 3.25
size in mb: 0.003173828125

Some Considerations

The os.stat method is equivalent to os.f_stat() in Python 3.3+. This method now takes an integer value as an argument—referencing a file descriptor—and will throw an TypeError if passed a file path such as sample.txt as an argument. It’s worth noting that the os.stat() method can accept both file descriptor objects as well as strings representing a file path.

For Windows users, the os.stat() method can ignore file pointer chaining by passing along the keyword argument follow_symlinks=False which is True by default. In such cases, Python will now use the specified path rather than raising an error. Check the official documentation for more details.

Final Thoughts

Getting the size of a file in Python is critical to many I/O operations. Among the many uses, knowing the size of files can also be used to help track progress during operations such as downloads, uploads, or writing to disk. Both the os.stat and os.path.getsize approaches here will get the job done and are provided via the standard Python library. Hard to get any simpler or efficient than that!

alpharithms discord banner 1
Zαck West
Entrepreneur, programmer, designer, and lifelong learner. Can be found taking notes from Mother Nature when not hammering away at the keyboard.