Python Fundamentals / Functions and errors
Lesson 35 of 38
Reading files
open() opens a file. Using with closes it for you automatically. Loop over the file to read it line by line.
Key points
- with open(path) as f:
- 'r' read, 'w' write, 'a' append
- strip() removes the newline
with open("notes.txt") as f:
for line in f:
print(line.strip())Quiz · +10 XP
Why use with open(...)?
Log in to save progress and earn XP.