Account
Categories

readlines() Method


Definition:

When you use this method in a program, it reads all lines from a file and returns them as a list.

If you pass a number as a parameter, it reads approximately that many bytes from the file; otherwise, it reads all lines from the file.

Syntax:

file_object.readlines()

Example:

file = open("report.txt", "r")

lines = file.readlines()

for line in lines:
    print(line)

file.close()

Suppose report.txt contains:

Rahul Kumar
Priya Sharma
Sohan Khanna
Lata Singh

Output:

Exercise

Write a Python program using readlines() method to read all lines from a file.