Easy Tutorial
❮ Python Math Ref Math Fsum ❯

Python3 File readline() Method

Python3 File Methods


Overview

The readline() method is used to read an entire line from the file, including the "\n" character. If a non-negative number is specified as an argument, it returns the specified number of bytes, including the "\n" character.

Syntax

The syntax for the readline() method is as follows:

fileObject.readline();

Parameters

-

size -- The number of bytes to read from the file.

Return Value

Returns the bytes read from the string.

Example

The following example demonstrates the use of the readline() method:

The content of the file tutorialpro.txt is as follows:

1:www.tutorialpro.org
2:www.tutorialpro.org
3:www.tutorialpro.org
4:www.tutorialpro.org
5:www.tutorialpro.org

Reading the content of the file:

Example

#!/usr/bin/python3

# Open file
fo = open("tutorialpro.txt", "r+")
print("File name: ", fo.name)

line = fo.readline()
print("First line read: %s" % (line))

line = fo.readline(5)
print("String read: %s" % (line))

# Close file
fo.close()

The above example outputs the following results:

File name:  tutorialpro.txt
First line read: 1:www.tutorialpro.org

String read: 2:www

Python3 File Methods

❮ Python Math Ref Math Fsum ❯