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 Python Knowledge

Python Reference Manual

Python file read() usage and examples

Python File (File) Methods

Overview

read() This method is used to read a specified number of bytes from the file, or all if not specified or negative.

Syntax

The syntax of the read() method is as follows:

fileObject.read();

Parameter

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

Return value

Returns the bytes read from the string.

Example

The following example demonstrates the use of the read() 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 file
f = open("w3codebox.txt", "r")
print(f.read(20))

The output result of the above example is as follows:

1:fr.oldtoolbag.com
2:ww

Python File (File) Methods