How to read a File Line By Line in Python?

Python has large numbers of inbuilt functions and modules that are used to perform different operations on the file, image, numbers, etc. In Python, Inbuilt functions such as write(), open(), close(),read() are used to process any file and perform certain operations on it. This write-up will provide all the methods used to read file line by line in Python. The content of this post is as follows::

So, let’s begin!

Method 1: Using the readlines() Function

The “readlines()” function reads all the lines in a single execution. This function is not used for large files due to executing a complete file at once. Let’s understand the working of the “readlines()” function via the example of code given below:

Note: In this example, the file named “itslinuxfoss” is being used to apply the “readlines()” function to read a file line by line.

Code

# Using readlines() to read the file line by line

file_name = open('itslinuxfoss.txt', 'r')
Lines = file_name.readlines()

count = 0
# Separating the newline character
for line in Lines:
    count += 1
    print("Line{}: {}".format(count, line.strip()))

In the above code:

  • The “open()” function opens the file and stores it in the variable named “file_name”.
  • The “readlines()” function read the input file and stores the result in the variable “Lines
  • The value of the “counter” is initialized to “ZERO” because we need to start reading the lines from the beginning of the file.
  • For loop” is used for iteration of lines on the file named “itslinuxfoss”.
  • The “count” value is increased whenever one line is read by the “readlines()” function.
  • The “format()” function is used to add the value of “count” and “lines” inside the string.
  • line.strip()” function strips the newline character when the “for loop” iterates over the file.

Output

Following the file’s content named “itslinuxfoss“, the output shows that the content is read and displayed on the output.

Method 2: Using readline() Function

The inbuilt function “readline()” also reads lines of file. The “readline()” reads one line at a time and does not read the complete file simultaneously. However, it can be used with the “while” and “if ” conditions to read all the lines simultaneously.

The following file given below is used here. The file named “itslinuxfoss” comprises three lines as shown below:

Let’s understand the functionality of the “readline()” function to read a file line by line: 

Code

# Using readline() to read the file line by line

file_name = open('itslinuxfoss.txt', 'r')
count = 0

while True:
    count += 1
    line = file_name.readline()

    # no line left then end of file
    if not line:
        break
    print("Line Number{}: {}".format(count, line.strip()))

file_name.close()

In the above output:

  • The “open()” function is used to open the file named “itslinuxfoss” and store it in the variable named “file_name”.
  • The value of the “counter” is set to “ZERO” to start the reading from the first line.
  • The “while” condition executes or becomes “True” when the file is opened.
  • The “count” value is increased with a step size of “1” to read a new line every time by using the “readline()” function.
  • When the file lines reach the end, the file will “break” and print the result of all lines on the console output.
  • The “format()” function is used to add the value of “count” and “lines” inside the string.
  • The “line.strip()” function strips the newline character.
  • close()” function closes the file because if the file is opened, then it must be closed after operating.

Output

The output shows that the file’s content is displayed in a line-by-line format.

Method 3: Using for loop

The “for loop” method is the simplest and easiest method among all others. Any file can be easily read line by line using the “for loop” instead of using the “readline()” function. Let’s see an example code below to read the file named “itslinuxfoss” using “for loop”.

Code

file_name = open('itslinuxfoss.txt', 'r')
count = 0

for line in file_name:
    count += 1
    print("Line{}: {}".format(count, line.strip()))

file_name.close()

In the above code:

  • The “open()” function is used to open the file named “itslinuxfoss” and store it in the variable named “file_name”.
  • The “counter” value is set to “ZERO”.
  • For loop” is used to iterate the file line named “itslinuxfoss”.
  • The “count” value is increased whenever the “for loop” iterates over one line.
  • The “format()” function is used to add the value of “count” and “lines” inside the string of print function.

Output

In the above output, the “file” is read line by line using “for loop”.

Method 4: Using List Comprehension and With Statement

“List comprehension” defines the “for loop” expression indeed the square bracket. List comprehension is used to create the list by simply executing its elements. “With” statements are used before the “open()” function to close the file explicitly whenever it is opened. The “with” statements handle exceptions or errors and make our program more readable. The “file.close()” is not used in the program when the “with” statement is used. Let’s understand this method with an example of code given below:

Code

#Using List Comprehension and With Statement
with open('itslinuxfoss.txt') as f:
    file_name = [line for line in f]

print(file_name)

# removing the new line characters
with open('itslinuxfoss.txt') as f:
    file_name = [line.rstrip() for line in f]

print(file_name)

In the above code:

  • The “open()” function opens the file named “itslinuxfoss” using “with statement”.
  • The “List Comprehension” expression is placed inside the bracket and defined using a “for loop” to iterate over the file.
  • The “print” function prints the lines of the file, including the “\n” newline character.
  • To remove the “newline” character, the file is opened again using the “open()” function. The “rstrip()” function removes the new line character of every line.
  • The final output is printed again without the “\n” character.

Output

In the above output, the “file” is read line by line with and without the “\n” newline character.

Conclusion

In Python, the “readlines()”, ”readline()”, ”for loop” and “List Comprehension” methods are used to read a file line by line. The “readlines()” are used to read all the lines of a file at single execution, and the “readline()” can read one line at a time. The “for loop” reads the file by iterating through the list. Moreover,  “List Comprehension” alongside “with” statements are used to read the file concisely. This article explained all the methods to read the file in Python with appropriate examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×