How to Read File Line by Line in Python – Definitive Guide

Python provides inbuilt libraries to handle files operation such as create, read, update, delete from the Python application.

You can read file line by line in python using the readlines() method.

In this tutorial, you’ll learn how to open and read files line by line in python using different methods.

If You’re in Hurry…

Here is the example to read file line by line into the list.

with open("full_file_path_With_extenstion",  'r', encoding='utf8' ) as f:
    content = f.readlines()
print(content)

If You Want to Understand Details, Read on…

Before you start reading the file, you need to open the file in Python. You can use open() method to open the file.

Files can be opened in different modes by specifying the parameters.

"r" – Opens the file in read Mode.

"a" -Opens file in Append mode. Creates a file if it’s not existing

"w" – Opens file in write mode. Creates a file if it’s not existing

"x" – Creates the file and opens it for processing. throws an error if a file with the same name already exists.

You can also specify the encoding of the file while opening it.

Encoding is a representation of a set of characters which can be very useful when you handle files with special characters like Ä in German language or something other language characters.

Sample File contents

This is first line
this is second line

This is third line after empty line

This is fourth line after empty line

This is fifth line with special character Ä

#this is a commented line which should be ignored while reading

This is sixth line after a commented line

Open File Syntax

open("file_name_with_extension", 'r', encoding='utf8') as f:

where

  • file_name_with_Extension – Full path of the file name
  • r – To open the file in the read mode. This is optional and file will be opened in read mode by default.
  • encoding – Specify the encoding of the file which can be used to read the file with correct charsets. utf8 is the most commonly used encoding.

Now the file will be opened and stored in the f file object.

Using ReadLines() Method

readlines() method is used to read one complete line from the file. It appends \n character at the end of each line read.

Syntax

file.readlines(sizehint)

Parameters

It accepts an optional parameter sizehint.

If you specify sizehint, whole lines totaling to sizehint bytes will be read instead of reading up to the end of the file.

Read file line by line into list

In this section, you’ll learn how to read the file line by line into a list with open file statement and readlines().

First, open the file with an open statement and create a file object.

Then use readlines() method in the file object to read the complete lines of the file.

It’ll read the file line by line and return a list as shown in the below example.

Example

with open("c:\temp\Sample_File.txt",  'r', encoding='utf8' ) as f:
    content = f.readlines()
print(content)

Output

['This is first line\n', 'this is second line\n', '\n', 'This is third line after empty line\n', '\n', 'This is fourth line after empty line\n', '\n', 'This is fifth line with special character Ä\n', '\n', '#this is a commented line which should be ignored while reading\n', '\n', 'This is sixth line after a commented line']

Using file Read method

You can use the file read() method to read the file line by line into an array with open file statement.

Python doesn’t have inbuilt support for arrays. But the lists can be used instead, which is similar to the array.

The code below demonstrates how to read file into an array.

Example

with open("c:\temp\Sample_File.txt",  'r', encoding='utf8' ) as f:
    filecontentasarray = f.read()
print(filecontentasarray)

Output

This is first line
this is second line

This is third line after empty line

This is fourth line after empty line

This is fifth line with special character Ä

#this is a commented line which should be ignored while reading

This is sixth line after a commented line

Read file line by line into tuple

In this section, you’ll learn how to read files line by line into tuples.

You can pass the file object directly into the tuple constructor while creating a file object using the open statement.

With this, it’ll yield a tuple with the lines from the file and you need not use the readlines() or read() method explicitly.

When to use

Tuples are unchangeable and ordered.

You can use it when you want to create an unchangeable record from the file and the order of the lines must be maintained.

Example

linesastuple = tuple(open("c:\temp\Sample_File.txt", 'r'))
print(linesastuple)

Output

('This is first line\n', 'this is second line\n', '\n', 'This is third line after empty line\n', '\n', 'This is fourth line after empty line\n', '\n', 'This is fifth line with special character Ä\n', '\n', '#this is a commented line which should be ignored while reading\n', '\n', 'This is sixth line after a commented line')

Read file line by line backwards

In this section, you’ll learn how to read files line by line backward or in reverse order.

First, you’ll read the file into the list and use the reversed method available in the list to iterate the list items in the reverse order.

reversed() method will return a revered iterator object which can be iterated using the for loop and access the file contents from the reverse order.

Example

for line in reversed(list(open('c:\temp\Sample_File.txt', encoding='utf8'))):
    print(line.rstrip())

Output

This is sixth line after a commented line

#this is a commented line which should be ignored while reading

This is fifth line with special character Ä

This is fourth line after empty line

This is third line after empty line

this is second line
This is first line

This is how you can read a file line by line in python backward or read a file from the end of the file. There is no specific method available to read files in the reverse order directly.

Conclusion

In this tutorial, you’ve learned how to read files line by line in python in different available ways.

If you know a different way other than the information available here, feel free to comment below.

You May Also Like

Leave a Comment