Home >>Python File Methods >Python File readlines() Method

Python File readlines() Method

Python File readlines() Method

The readlines() method in python file is used to returns the list containing each line in the file. Generally, no more lines are returned, if the total number of bytes returned will exceed the specified number.

Syntax:
file.readlines(hint)

Parameter Values

Parameter Description
hint This parameter is used the Number of lines to be read from the file. -1 is a default value, refers to all lines will be returned.
Here is an example of Python File readlines() Method:

x = open("file1.txt", "r")
print(x.readlines())

Output:
['Hello! Welcome to file1.txt\n', 'This file is for testing purposes.\n', 'Good Luck!']
Example 2:

y = open("file2.txt", "r")
print(y.readlines())

Output:
['Hello! Welcome to file2.txt\n', 'This file is for testing purposes.\n', 'Good Luck!']

No Sidebar ads