Learning Python can be complicated and difficult, but it is also one of the most valuable programming languages to learn. However, working with Python comes with working with lots of files. In this article, we will learn how to get the size of a file in Python using various methods with examples. So, let’s get started!
4 Methods to Get File Size in Python
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. Below are the 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:
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).
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.
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.
How do you find the MB size in Python?
In Python, you may ascertain the size of an object by using the sys module, which offers access to several variables utilized or maintained by the interpreter as well as functions that interact heavily with the interpreter.
The getsizeof() function from the sys module can be used to calculate the size of an object in memory. This function usually returns the size of the object in bytes. An example code on how to find the size of an object using python:
In the above example, we have imported the sys module and created an integer list. The getsizeof() function is then used to determine the size of the list object in memory and stores the result in the size variable. Finally, we obtain the result, which is the size of the list object in bytes.
Get Size of all Files in a Directory
The os and os.path modules in Python can be used to get the size of all files in a directory. The os module contains functions for interacting with the operating system, and the os.path module has functions to work with paths.
An example code that demonstrates how to get the size of all files in a directory:
In the above code, we begin by importing the os module.
- And in the next step we have specified the directory whose files we wish to know the size of by setting the directory variable to the directory's path.
- To retrieve a list of all the files in the directory, we use the os.listdir() function.
- We have used a for loop to cycle over the list of files and get the path of each file by joining the directory path and the file name with the os.path.join() function.
- Using the os.path.isfile() function, we determined whether the file is an ordinary file (rather than a directory).
- If the file is a regular file, we use os.path.getsize() to get the file size in bytes and add it to the total_size variable.
- Finally, we have printed the total size in bytes of all the files in the directory.
In this article, we studied how Python checks file size using four different methods as well as examples and output. If you still have any doubts, our Python Tutors are available 24/7 online to help you.