Python File readline() Method

❮ File Methods


Example

Read the first line of the file "demofile.txt":

f = open("demofile.txt", "r")
print(f.readline())
Run Example »

Definition and Usage

The readline() method returns one line from the file.

You can also specified how many bytes from the line to return, by using the size parameter.


Syntax

file.readline(size)

Parameter Values

Parameter Description
size Optional. The number of bytes from the line to return. Default -1, which means the whole line.

More examples

Example

Call readline() twice to return both the first and the second line:

f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
Run Example »

Example

Return only the five first bytes from the first line:

f = open("demofile.txt", "r")
print(f.readline(5))
Run Example »

❮ File Methods

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.