Chapters
Python114 chapters

ReferenceFile methods

file.read()

Reads the whole file, or a given number of characters.

Signature

f.read(size=-1)

Parameters

NameMeans
sizehow much to read; everything when omitted

Returns

A string, or bytes in binary mode.

Example

Python
with open("notes.txt", "w", encoding="utf-8") as f:
    f.write("Ada,1815\n")

with open("notes.txt", encoding="utf-8") as f:
    print(repr(f.read(3)))
    print(repr(f.read()))
    print(repr(f.read()))

Output

'Ada'
',1815\n'
''

See also