English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Fonctions en Python

Types de données en Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python readline() usage and examples

Python File (File) Methods

Overview

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

Syntax

readline() method syntax is as follows:

fileObject.readline(size)

Parameter

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

Return value

Return the bytes read from the string.

Example

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

File w3The content of codebox.txt is as follows:

1:fr.oldtoolbag.com
2:fr.oldtoolbag.com
3:fr.oldtoolbag.com
4:fr.oldtoolbag.com
5:fr.oldtoolbag.com

Loop to read the content of the file:

# Open the file
fo = open("w3codebox.txt, \
print("The file name is: ", fo.name)
line = fo.readline()
print("Read the first line %s" % (line))
line = fo.readline(5)
print("The string read is: %s" % (line))
# Close the file
fo.close()

The output result of the above example is:

The file name is:  w3codebox.txt
Read the first line 1:fr.oldtoolbag.com
The string read is: 2:www

Python File (File) Methods