Python File read() Method

Example

Read the content of the file "demofile.txt":

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

Run Example

Definition and Usage

The read() method returns the specified number of bytes from the file. The default value is -1, indicating the entire file.

Syntax

file.read()

Parameter Value

Parameter Description
size Optional. The number of bytes to return. The default value is -1, indicating the entire file.

More Examples

Example

Read the content of the file "demofile.txt":

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

Run Example