Lazo Lab Sign up

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())
Watch a video on thisOpens YouTube search results for “Reading files” in a new tab

Quiz · +10 XP

Why use with open(...)?

Log in to save progress and earn XP.