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
| Name | Means |
|---|---|
file | the path |
mode | r read, w write, a append, x create, add b for bytes |
encoding | name 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