Skip to content
Built-in Functions

open()

Open a file so you can read from it or write to it.

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

open() gives you a file object. A with statement closes the file automatically when its block ends.

Use mode "w" to write and the default mode "r" to read.

Parameters

NameDescription
fileThe path of the file to open.
modeHow to open it, such as "r" for reading or "w" for writing.
encodingOptional text encoding, often "utf-8" for text files.

Returns

A file object.

Try it

Run it and change it
import os, tempfile
with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "note.txt")
    with open(path, "w") as file: file.write("hello")
    with open(path) as file: print(file.read())

Related entries