Python Program to Read Lines from Text File and Display those Lines of Length More than 25

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 programme 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 read lines from the given file and print those lines of length(characters) more than 25 in Python

Program to Read Lines from Text File and Display those Lines of Length More than 25 in Python

Below is the full approach for reading lines from the given file and printing those lines of length(characters) more than 25 in Python.

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.
  • Pass the given filename, r (represents read-only mode) as arguments to the open() function to open the given file.
  • Iterate in the lines of the file using the for loop.
  • Calculate the length of the line using the len() function and store it in a variable.
  • Check if the length of the line is greater than 25 using the if conditional statement.
  • If it is true, then print that respective line.
  • 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"
# Pass the given filename, r (represents read-only mode) as arguments to the open() function
# to open the given file
file = open(givenFilename,"r")
print("The lines having more than 25 characters are:")
# Iterate in the lines of the file using the for loop
for lines in file:
    # Calculate the length of the line using the len() function and store it in a variable.
    line_len=len(lines)
    # Check if the length of the line is greater than 25 using the if conditional statement
    if line_len>25:
        # If it is true, then print that respective line.
        print(lines)

Output:

The lines having more than 25 characters are: 
GoodMorning 123 this is 567 Btechgeeks @#% 

hello this is Btechgeeks.welcome all

Explanation:

  • The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
  • Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
  • The file will be opened in reading mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
  • When we open the file, we use the character ‘r’ to signify read-mode.
  • We traverse through the lines of the file using For loop and check the length of the corresponding line using the len() function.
  • We print the lines which are having length greater than 25.

File Content:

hello all
GoodMorning 123 this is 567 Btechgeeks @#%
pythonprograms
hello this is Btechgeeks.welcome all
welcome to btechgeeks
btechgeeks 
pythoncode

Google Colab Images:

Files and Code: