Chapters
Python114 chapters

ReferenceBuilt-in functions

open()

Opens a file and returns a file object. Use it with with so it always closes.

Signature

open(file, mode='r', encoding=None)

Parameters

NameMeans
filethe path
moder read, w write, a append, x create, add b for bytes
encodingname it explicitly; the default varies by platform

Returns

A file object.

Example

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

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

Output

one line

See also