Account
Categories

readline() Method


Definition:

When you use this method in a program, it reads one line from a file.

If you pass a number as a parameter, it reads only that many characters from the line; otherwise, it reads the entire line.

Syntax:

file_object.readline(size)

Example:

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

line1 = file.readline()
print(line1)

line2 = file.readline(8)
print(line2)

file.close()

Suppose report.txt contains:

Rahul Kumar
Priya Sharma

Output:

Rahul Kumar
Priya S

Exercise

Create a file named report.txt and use the readline() method to read one line from the file.