
Lesson 06 · 10 min
Files and with
with opens and closes by itself. Reading text — specify the encoding.
with open("notes.txt", encoding="utf-8") as f:, then f.read() or a loop over lines. Leave the with and the file is closed, even if there was an error inside. Without with it's easy to forget close() and hit "too many open files".
Sign in to continue
The rest of this lesson opens after sign up
The first lesson of every course is open without an account. To read on, solve the practice and use Ask, create a free account or log in.