If you’re just starting with Python and want to know how to find the size of a file, you’re in the right place. Knowing the size of a file is often important when you’re working with large files, managing resources, or optimizing your program.
In this guide, we will show you how to easily find the size of a file in Python using the os module.
Let’s dive into it.
What is a File Size?
Before we jump into the code, let’s quickly understand what file size means.
The size of a file is simply the amount of data stored in that file. It could be measured in bytes, kilobytes (KB), megabytes (MB), or gigabytes (GB).
For example, a simple text file might have a size of 1 KB, whereas a video file could be several gigabytes.
Why Would You Need to Know the Size of a File?
Here are a few reasons why knowing the size of a file can be useful——
- Handling Large Files: If you’re working with large files (like videos or images), you might want to know their size to decide whether to process them.
- Storage Management: If you’re managing a system or working with disk storage, knowing file sizes can help you keep track of space usage.
- Optimizing Code: In some cases, you may need to decide whether to load or upload a file based on its size.
How to Find the Size of a File in Python
Python makes it super easy to find the size of a file using the os.path.getsize() function from the os module. Let’s go through the steps and see how to do this.
- Step 1: Import the os Module
The os module provides a way to interact with the operating system. To get the size of a file, we will need to use the os.path.getsize() function, so the first thing you need to do is import this module.
import os
- Step 2: Use os.path.getsize() to Get the File Size
The getsize() function takes the file path as an argument and returns the size of the file in bytes. Here’s how you can use it:
import os
# Replace 'yourfile.txt' with the path of the file you want to check
file_size = os.path.getsize('yourfile.txt')
print(f"The size of the file is: {file_size} bytes")
This will print the size of the file in bytes.
- Step 3: Convert the Size to More Understandable Units
While the size is returned in bytes, it’s often more helpful to convert it to other units, like kilobytes (KB) or megabytes (MB). You can easily do this by dividing the size by 1024 (for KB) or 1024² (for MB).
Here’s an example:
import os
# Replace 'yourfile.txt' with the path of your file
file_size = os.path.getsize('yourfile.txt')
# Convert size to kilobytes (KB)
file_size_kb = file_size / 1024
# Convert size to megabytes (MB)
file_size_mb = file_size / (1024 * 1024)
print(f"The size of the file is: {file_size} bytes")
print(f"The size of the file is: {file_size_kb:.2f} KB")
print(f"The size of the file is: {file_size_mb:.2f} MB")
Result for above code will be like
Let’s say your file size is 5,000 bytes. The output will look something like this:
- The size of the file is: 5000 bytes
- The size of the file is: 4.88 KB
- The size of the file is: 0.00 MB
This makes it easier to understand the file size in a more familiar unit like KB or MB.
Final thought
Finding the size of a file in Python is simple and straightforward using the os.path.getsize() function.
Whether you’re handling images, videos, or text files, this function helps you quickly check how much space a file occupies.
If you’re new to Python, this is a great way to start practicing file handling and interacting with the file system.
As you continue learning, you’ll find that file sizes are an important part of working with files in your programs.