Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check the file size?
Files are used to store certain data based on user requirement. A file can contain any type of information such as audio, text, images, video or program. Depending on the data present in a file, the size of a file varies.
The size of a file is measured in bits or bytes. A bit is the smallest unit of information which is typically represented by a single value: either 0 or 1. Bytes are used to measure the amount of data in a file and the bytes are denoted by 8 bits. There are several ways to check the file size in Python ?
Using os.path.getsize()
The os.path module in Python provides a function named getsize(), which is used to find the size of the file.
Syntax
from os import path path.getsize(file_name)
Where,
os is the module in Python.
path is the method in os module.
getsize is the function used to calculate the file size.
file_name is the input file path.
Example
In this example, we will create a sample file and then check its size using os.path.getsize() ?
import os
from os import path
# Create a sample file
with open("sample.txt", "w") as f:
f.write("Hello, this is a sample file content!")
# Get file size
file_location = "sample.txt"
file_size = path.getsize(file_location)
print("File size in bytes:", file_size)
# Clean up
os.remove("sample.txt")
File size in bytes: 37
Using os.stat()
In Python, os.stat() method accepts a file path as an argument and returns a stat object containing various attributes describing its structure. The st_size attribute contains the size of the file.
Example
In the following example we are retrieving the size of a file using st_size attribute ?
import os
# Create a sample file
with open("sample.txt", "w") as f:
f.write("This is sample content for testing file size.")
# Get file size using os.stat()
file_path = "sample.txt"
file_stats = os.stat(file_path)
file_size = file_stats.st_size
print("File size in bytes:", file_size)
# Clean up
os.remove("sample.txt")
File size in bytes: 45
Using pathlib Module
Python also provides the pathlib module, which offers an object-oriented approach to handle file paths. The stat().st_size method can be used to calculate the size of a file.
Example
from pathlib import Path
# Create a sample file
sample_file = Path("sample.txt")
sample_file.write_text("Sample content using pathlib module!")
# Get file size
file_size = sample_file.stat().st_size
print("File size in bytes:", file_size)
# Clean up
sample_file.unlink()
File size in bytes: 37
Using os.fstat()
The os.fstat() method is used to retrieve the status of a file descriptor. It accepts a file descriptor as an argument and returns a stat object with attributes describing the file status. You can retrieve the file size using st_size.
Example
import os
# Create a sample file
with open("sample.txt", "w") as f:
f.write("Content for testing os.fstat() method.")
# Get file size using os.fstat()
file_path = "sample.txt"
with open(file_path, "rb") as f:
file_stats = os.fstat(f.fileno())
file_size = file_stats.st_size
print("File size in bytes:", file_size)
# Clean up
os.remove("sample.txt")
File size in bytes: 36
Comparison
| Method | Use Case | File Handle Required |
|---|---|---|
os.path.getsize() |
Simple and direct | No |
os.stat() |
Additional file metadata | No |
pathlib |
Object-oriented approach | No |
os.fstat() |
Working with file descriptors | Yes |
Conclusion
Use os.path.getsize() for simple file size checks. Use pathlib for modern, object-oriented file operations. Use os.fstat() when working with open file descriptors.
