You will sometimes encounter the need to measure the size of files from a Python program. Here are 5 ways to do so!
Method 1: Use os.path.getsize()
In Python, there is a module called “os” (for operating system) that is used for a lot of interfacing functionality with the underlying operating system. Within “os”, “os.path” supports various pathname based manipulations. Finally, getsize() obtains the file size in bytes. Here is sample code:
Here “main.py” refers to this program itself. Depending on your Python implementation, you might need to give the full path name rather than just “main.py”. The output of this program is:
Here 44 refers to the number of bytes in the program, with one byte storing one character.
If you specify a filename that doesn’t exist, such as:
we will get the output:
Method 2: Use os.stat(<filaneme>).st_size
This is a second way to find the sizes of files but again using the os module in Python. But note that this uses a different library than “path”. Here we use “stat”.
The corresponding code looks like:
The output will be the same as before:
Method 3: Iterate through the file and count bytes
This is a back-to-basics approach wherein we open the file and begin reading it byte and byte. As we keep reading the file, we increment a counter within our program. Once we reach the end of the program, we print the result. Here is the program:
Note that the program does not use any libraries (hence the “back to basics” approach). We use a while loop to read one byte at a time (the read(1) statement). If that byte read does not exist we exit (break) out of the while loop. Else, we increment our counter.
This program will print (at the end, in-between prints not shown for brevity):
Note that the size of the program (which is now 146 bytes) has changed because it is a different program.
Method 4: Use pathlib.Path(<filename>).stat().st_size
This method is similar to method 2 above but uses a different module (namely, pathlib instead of os).
The output will be:
Again, note that the file size has changed because the program has changed.
Method 5: Use seek and tell to find filesize
This method is similar to method 3 but not as first principles as that approach. We use seek to first position the file pointer. Such positioning can be done either w.r.t. The beginning of the file or the end of the file. Once the positioning is done we can use tell to count the bytes read thus far and thus the filesize. Here is a program that encapsulates this logic:
(again note that the file has changed and thus the file size is different.)
There you have it - fie different ways to get the file size in your Python program! Which one is your favorite?
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.