open()

Opens a file and returns a file object. Use the with statement for automatic cleanup.

Syntax

python
open(file, mode="r", encoding=None)

Example

python
with open("data.txt", "r", encoding="utf-8") as f:
    content = f.read()

with open("output.txt", "w") as f:
    f.write("Hello, file!")