Learning Python can be complicated and difficult, but it is also one of the most valuable programming languages to learn. This is particularly true for anyone who wants to work in the field of data analysis, whether it's with a data analytics firm, as a financial analyst, or in another industry. However, working with python comes with working with lots of files. In this article, we will learn how python get file size with various methods and examples in detail below. So, let’s get started!
How does Python Get File Size?
Below are four methods by which python gets file size:
Method 1: Using the os.path module
Python’s os.path module provides a getsize() function which returns the file size as the integer. Here, the function takes the file path as the argument and returns the size of the file in bytes. Check out the below example for a better understanding:
For example:
import os file_size = os.path.getsize('d:/test_file.jpg') print("File Size is :", file_size)
Output:
Size of file is : 512
Method 2: Using stat function
Python’s os module provides the stat function which returns an integer as file size. This function takes a file path as the argument which may be a string or object and returns statistical details about the path provided. These statistical properties provide with “st_size” parameter which contains the size of the file(in bytes).
For example:
import os file_size = os.path.getsize('d:/test_file.jpg') print("File Size is :", file_size)
Output:
Size of file is : 512
Method 3: Using file object
Python get file size using file object by following the below steps:
- To open the file, use the open() function and save the resulting object in a variable. When you open the file, the cursor moves to the beginning of the file.
- The file object supports the seek() method to move the pointer to the desired position. Simply put, it is used to set the cursor to the file’s end position. It takes two arguments: start and finish location.
- The file object contains a tell() method that may be used to retrieve the current cursor location, which is equal to the number of bytes changed by the cursor. As a result, this function returns the file's size in bytes.
For example:
file = open('d:/test_file.jpg') file.seek(0, os.SEEK_END) print("File Size is :", file.tell())
Output:
Size of file is : 512
Method 4: Using the pathlib module
The Path object's stat() function returns file characteristics such as "st_mode", "st_dev", and so on. Furthermore, the stat method's "st_size" property returns the file size in bytes. Check out the below example for a better understanding.
For example:
from pathlib import Path Path(r'd:/test_file.jpg').stat() file=Path(r'd:/test_file.jpg').stat().st_size print("File Size is :", file)
Output:
Size of file is : 512
Conclusion
In Python, determining the size of a file is crucial for many I/O tasks. Knowing the size of files may be useful for several things, including tracking progress during operations like downloads, uploads, and writing to disc. In this article, we studied how python check file size using four different methods as well as examples and output. If you have any doubts about your Python homework, our tutors are available 24/7 to help you.