Python Program to Find Size of a File

This article is created to cover some programs in Python, that find and prints the size of a file entered by user at run-time. Here are the list of programs covered in this article:

Find Size of File using getsize()

The question is, write a Python program that finds size of file using getsize() method. The program given below is answer to this question:

import os

print("Enter the Name of File: ")
fileName = input()
sizeOfFile = os.path.getsize(fileName)
print("\nSize of Given File (in bytes) is:")
print(sizeOfFile)

Here is its sample run:

python find size of file

Now supply the input as name of file to find and print its size. Here is its sample output with user input fresherearth.txt. This file is already exists in the current directory. That is, the folder where the above program (source code) is also saved:

find size of file python

Here is the window that shows the details along with size of the same file as provided in sample run:

calculate size of file python

And the snapshot given below shows the content of same file:

python find file size

Note - As you can see from the content of file, fresherearth.txt. There are 92 characters (with spaces) and 2 newlines (that can not be seen). The second and third line of texts has newlines. That is after first line, there is a newline, and then after second line, there is a newline.

Find Size of File with User-based Code

The question is, write a program in Python that find and prints the size of file without using any pre-defined function. The following program is the answer to this question:

print(end="Enter File's Name: ")
fileName = input()
try:
  fileHandle = open(fileName, "r")
  sizeOfFile = 0
  for line in fileHandle.readlines():
    sizeOfFile = sizeOfFile + (len(line)+1)
  sizeOfFile = sizeOfFile-1
  print("\nFile's Size = " +str(sizeOfFile)+ " bytes")
except IOError:
  print("\nThe File doesn't exist!")

Here is its sample run with same file name say fresherearth.txt:

find file size python

Here is another sample run with user input, fresherearth.py (the name of current Python program source code's file):

python program find size of file

Find Size of File in KB, MB, GB, TB

Now this is the final program of this article, that find and prints the size of file in KB, MB, GB, TB. Let's have a look at the program and its sample output:

print(end="Enter File's Name: ")
fileName = input()
try:
  fileHandle = open(fileName, "r")
  sizeOfFile = 0
  for line in fileHandle.readlines():
    sizeOfFile = sizeOfFile + (len(line)+1)
  sizeOfFile = sizeOfFile-1
  kb = sizeOfFile/1024
  if kb>1:
    mb = kb/1024
    if mb>1:
      gb = mb/1024
      if gb>1:
        tb = gb/1024
        if tb>1:
          tb = "{0:.2f}".format(tb)
          print("\nFile's Size = " +str(tb)+ " TB")
        else:
          gb = "{0:.2f}".format(gb)
          print("\nFile's Size = " +str(gb)+ " GB")
      else:
        mb = "{0:.2f}".format(mb)
        print("\nFile's Size = " +str(mb)+ " MB")
    else:
      kb = "{0:.2f}".format(kb)
      print("\nFile's Size = " +str(kb)+ " KB")
  else:
    print("\nFile's Size = " +str(sizeOfFile)+ " bytes")
except IOError:
  print("\nThe File doesn't exist!")

Here is its sample run with user input, codes.txt:

python find file size in kb mb gb

Here is the snapshot of properties of codes.txt file:

find file size in kb mb gb python

Python Online Test


« Previous Program Next Program »