Python Programming 2 Lesson 5.1: Read and Write Text Files
Python Programming 2 · Unit 5 · Lesson 5.1

Read and Write Text Files

Python Programming 2 Grades 9–12 5-day lesson 33 deliverables

Teach students to make Python programs read a text file, write one and add to one. Students loop over the lines of a reading list, skip blank lines, count books and add up pages in a reading log. They pick the right mode for four jobs, save game scores in a file and check that a file exists before the program reads it or deletes it.

Single lesson
$13

● Instant download · Lifetime access · Editable & print-ready

Print + digitalPDF, PowerPoint, Word & Google
Answer keysA key for every page
Built-in assessmentExit tickets + lesson quiz
45-minute daysEvery day planned and timed
Free for your students

5 coding levels come with this

See the levels

Short coding levels practice this lesson in CodeGrove, our code lab that runs in the browser. Students write the program, run it and check it, with no account and nothing to install. Use them with the lesson or as practice later.

See the levels
What's inside

What does this lesson cover?

Each line read from a file ends with a newline, and write mode can erase a file when students only meant to add to it. This lesson has students trace a loop over a reading list and choose "w" or "a" for each job. They finish by fixing a visitor log program.

The try-its on Days 1 and 3, the labs on Days 2 and 4 and the quiz task on Day 5 run online in CodeGrove, our in-browser code lab, so students need a computer or tablet on those days. The Reading Log and High Score File levels practice reading a text file line by line, skipping blank lines, writing and adding to a file, and checking that a file exists before the program reads it or deletes it. CodeGrove is free, with no accounts, sign-ins or installs.

Featured original activity

Reading Log

Students write functions in CodeGrove that count the books in a reading log and add up their pages.

33 deliverables

Everything that's included

00 Start Here

  • READ FIRST: every file in the lesson and when you use it
  • Teacher Guide and Pacing: the plan for each day, what to print and how to grade
  • Skills map: which standards each day teaches

01 Present

  • One editable PowerPoint deck for all five days, 36 slides, with speaker notes
  • Slide script: the speaker notes, printed
  • Bell ringers: a short warm-up for each day

02 Student Handouts

  • Guided notes: one packet for all five days, in PDF and editable Word. Students fill in the blanks during the slides, with the word bank on the page
  • Vocab slip: the lesson's words and what they mean, on one page

03 Labs

  • Reading Log: students write functions in CodeGrove that count the books in a reading log and add up their pages
  • High Score File: students save game scores in a file in CodeGrove, show them, and check for the file before they read it or delete it

04 Activities

  • Line by Line: students read a file with `with`, trace a loop over its lines on paper and practice in CodeGrove
  • Which Mode?: students pick the right mode for four jobs, predict what a program writes and practice writing files in CodeGrove
  • Quiz task: students fix a program that writes a file in CodeGrove, working alone, and then take the lesson quiz

05 Assessment

  • Exit tickets: quick questions that show who needs more help
  • Lesson quiz: 8 multiple-choice questions and 2 written answers about all five days, on paper

06 Answer Keys

  • 7 answer key files: a key for each day (its bell ringer, notes and exit ticket) and the activity and lab keys, plus the quiz key

Digital: Google Forms™ + Google Slides™

  • 6 Google Forms™ of the exit tickets and the quiz, ready to copy into your Drive
  • The multiple choice grades itself, and you grade the written answers
  • A Google Slides™ copy of the deck, with the same slides and speaker notes, for Google Classroom™
  • Give a Form that asks students to read code only on school-managed Chromebooks in locked mode, and use the paper version on other devices

Online: CodeGrove

  • In CodeGrove: the labs on Days 2 and 4 (Reading Log and High Score File) and the quiz task on Day 5, on a computer or tablet for each student
  • Try-its on Days 1 and 3: students practice the new code in CodeGrove, and nothing is turned in
  • Free to use, with no accounts, sign-ins or installs

Why teachers love this lesson

Each line keeps its newline

Students use line.strip() before they print or split a line, and they skip blank lines in a reading log.

The mode decides what happens to old data

Students see that "w" starts a file over while "a" adds to its end, and they add the newline to each line themselves.

Print and digital

Every page is a print-ready PDF, and the Google Forms™ and Google Slides™ let you teach the same lesson in Google Classroom™.

Real online labs

Students write and run real Python in CodeGrove, our in-browser code lab, on a computer or tablet, with no accounts or installs.

A key for every page

Every day has its own answer key, and every written question has a sample answer and what to look for.

Every day fits a 45-minute class

Each day's plan is timed activity by activity against real reading and writing speeds.

What Teachers Say

Be the first to review this

Reviews help me a lot, and they help other teachers pick the right resources for their classes. It takes about a minute.

Leave a review

Questions teachers ask

How do I get the files after I buy?

Instant download right after checkout: PowerPoint and Word files you can edit, print-ready PDFs, and links that copy the Google Forms™ and Google Slides™ into your Drive.

Can I buy the whole unit or course instead?

Yes. This lesson is part of the Unit 5 bundle and the complete Python Programming 2 course. Both cost less than buying the lessons one at a time.

Is this AP Computer Science?

No. It is a full-year, non-AP second course in Python for grades 9 to 12. For AP, see our AP Computer Science A and AP CSP courses.

What should students know before this lesson?

Students know loops, functions that return values, dictionaries, f-strings and \n.

Do students need computers?

Yes, on the CodeGrove days. Each student needs a computer or tablet that's online, and CodeGrove needs no account or install. The bell ringers, notes, exit tickets and quiz are on paper.

What formats are the files in?

Every page is a print-ready PDF. The slides are an editable PowerPoint and the guided notes an editable Word file. The exit tickets and the quiz also come as Google Forms™, and the deck as Google Slides™.

Which standards does it cover?

Written for Florida's Intermediate Programming course (9007220). The skills map in the lesson shows which standards each day teaches.