Python Program to Find Size of a File

Files in Python:

One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.

Also, if you didn’t know, I/O operations are the most expensive processes where a program can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.

Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.

Given a file, the task is to find the size of the given file in Python.

Program to Find Size of a File in Python

Method #1: Using Built-in Functions

Below is the full approach for finding the size of the given file in Python.

Approach:

  • Import os module using the import Keyword.
  • Make a single variable to store the path of the file. This is a constant value.
  • This value must be replaced with the file path from your own system in the example below.
  • Pass the above file as an argument to the stat() function of the os module and Store it in a variable.
  • Print the size of the above file using the st_size function.
  • The Exit of Program

Below is the implementation:

# Import os module using the import Keyword
import os
# Make a single variable to store the path of the file. This is a constant value. 
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Pass the above file as an argument to the stat() function of the os module and
# Store it in a variable
file = os.stat(givenFilename)
# Print the size of the above file using the st_size function
print("The size of the above file is:")
print(file.st_size)

Output:

The size of the above file is:
16

Google Colab Images:

Files and Code:

samplefile.txt:

Method #2: Using For Loop

Approach:

  • Make a single variable to store the path of the file. This is a constant value.
  • This value must be replaced with the file path from your own system in the example below.
  • Take a variable(which gives the length of the file) and initialize its value with zero.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Iterate through the lines of the file using the For loop.
  • calculate the length of the line using the len() function and increment the length variable with the length of the line.
  • Print the length of the given file.
  • The Exit of Program

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value. 
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Take a variable(which gives the length of the file) and initialize its value with zero.
length=0
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Iterate through the lines of the file using the For loop.
    for gvnfileline in givenfilecontent:
      #calculate the length of the line using the len() function and 
      #increment the length variable with the length of the line
      length+=len(gvnfileline)
# Print the length of the given file 
print("The length of the given file is:",length)

Output:

The length of the given file is: 16

File Content:

btechgeeks hello

Google Colab Images:

Files and Code: