Python File readlines() Method

Example

Returns all lines in the file as a list, where each line is an item in the list object:

f = open("demofile.txt", "r")
print(f.readlines())

Run Instance

Definition and Usage

The readlines() method returns a list that contains each line of the file as an item in the list.

Use the hint parameter to limit the number of returned lines. If the total number of returned bytes exceeds the specified number, no more lines will be returned.

Syntax

file.readlines(hint)

Parameter Value

Parameter Description
hint Optional. If the number of returned bytes exceeds hint Numbers, no more lines will be returned. The default value is -1, which means all lines will be returned.

More Examples

Example

If the total number of returned bytes is greater than 33, the next line will not be returned:

f = open("demofile.txt", "r")
print(f.readline(33))

Run Instance