Python is well known for its file management functionalities, i.e., removing files, adding files, and managing the size. Among these, the size of a file is an important factor to consider as the storage on the system or server has an effective impact on the overall performance of the system. To check the file size, various functions such as os.path.getsize(), os.stat(), etc are used in Python.
All these methods are briefly explained in this guide and are listed below:
- Method 1: Using os.path.getsize() Function
- Method 2: Using os.stat() Function
- Method 3: Using pathlib.Path.stat() Function
- Method 4: Using file.tell() Method
Method 1: Using os.path.getsize() Function
The “os.path.getsize()” function of the os module is used to get the file size. Here is an example code:
Code:
import os
path =r'C:\Users\p\Documents\program\example.py'
size = os.path.getsize(path)
print(f'The size of the file is {size} bytes')
- The module named “os” is imported.
- The complete “path” of the file is assigned to a variable named “path.”
- The “os.path.getsize()” function takes the path as an argument and retrieves the file size in bytes.
Output:
The specified size of the file has been displayed.
Method 2: Using os.stat() Function
The “os.stat()” function is used to get the status of the file or directory. The following example will help you understand:
Example 1: Check File Size
The below code is used the “os.stat()” function to check the file size:
Code:
import os
path =r'C:\Users\p\Documents\program\example.py'
size = os.stat(path)
print(f'The size of the file is {size.st_size} bytes')
- The “os.stat()” function takes the complete path of the file as an argument and returns the information related to files.
- The attribute “.st_size” of the “os.stat()” function is used to get the file size.
Output:
The file size has been displayed in the above snippet.
Example 2: Check File Size in Various Format Sizes
To check the file size in various sizes or formats such as KB, MB, and GB, the following code is used in Python:
Code:
import os
bytes_size = os.stat(r'C:\Users\p\Documents\program\windows.iso').st_size
kb_size = bytes_size / (1024)
mb_size = bytes_size / (1024 * 1024)
gb_size = bytes_size / (1024 * 1024 * 1024)
print(f'The size of the file is {kb_size} KB, {mb_size} MB, or {gb_size} GB')
- The “os.stat()” function is used to get the file size in bytes with the help of “st_size” attribute.
- The bytes size is divided by the integer value “1024” to obtain the kilobytes size of the file.
- Similarly, the MB and GB size is calculated using the byte size by performing simple division.
Output:
The file size in various formats has been calculated.
Method 3: Using pathlib.Path.stat() Function
The pathlib module in Python supports object-oriented handling of file system paths. The “pathlib.path.stat()” function is used in the below code to check the file size:
Code:
from pathlib import Path
path = Path(r'C:\Users\p\Documents\program\example.py')
size = path.stat()
print(f'The size of the file is {size.st_size} bytes')
- The “pathlib” module is imported at the start.
- The path() function of the pathlib module is used to get the file’s complete path.
- The “path.stat()” function returns the status of the file, including the file size and other information.
- The attribute “st_Size” is used to get the file size.
Output:
The size of the file has been displayed.
Method 4: Using file.tell() Method
The “file.tell()” method retrieves the current/present file position in a file stream. The below code gets the file size in Python:
Code:
with open(r'C:\Users\p\Documents\program\example.py', 'rb') as f:
f.seek(0, 2)
f_size = f.tell()
print(f'The size of the file is {f_size} bytes')
- The “open()” function is used to open the specified file in binary mode and read its contents.
- The f.seek(0, 2) method moves the file pointer to the end/last of the input file.
- The f.tell() method is then used to get the current/existing position of the file pointer, which is the file size in bytes.
Output:
The file size has been displayed.
Conclusion
To check file size, the “os.path.getsize()” function, “os.stat()” function, “pathlib.Path.stat()” function, and “file.tell()” method are used in Python. The “os.path.getsize()” function and “os.stat()” function of the os module accepts the complete file path as a parameter and retrieves the file size. The “pathlib.path.stat()” and “file.tell()” methods can also return the file size. This guide presented various methods for getting the file size in Python.