Lesson Summary


This lesson introduces students to reading information from an input file and writing to an output file as a functionality of Python programming. The students will then apply these concepts to program a simple Dice Roll application to generate data. This lesson will prepare students to read and write files for use in later Data Acquisition lessons.


  • Students will learn how to read from an input data file and write to an output file using the Python programming language.
  • Students will be able to apply their new knowledge to writing independent programs.


Session 1

  1. Getting Started (10 min) - Journal on the possible advantages/disadvantages of being able to input/output data.
  2. Book activity (40 min) - Students are guided through the properties of file I/O using the "Python for Everybody" book.

Session 2 

  1. Python Lab (50 min) - Students write a program to roll 2 six-sided dice and one 12-sided die and write the output to a file. They then import it to Excel as a txt or csv file and compare distributions using countif. (This exercise may be assigned as homework if students have the computing resources to complete a programming assignment as homework.)


Learning Objectives

CSP Objectives

Big Idea - Creativity
  • EU 1.2 - Computing enables people to use creative development processes to create computational artifacts for creative expression or to solve a problem.
    • LO 1.2.3 - Create a new computational artifact by combining or modifying existing artifacts. [P2]
Big Idea - Abstraction
  • EU 2.3 - Models and simulations use abstraction to generate new understanding and knowledge.
    • LO 2.3.1 - Use models and simulations to represent phenomena. [P3]
Big Idea - Data
  • EU 3.2 - Computing facilitates exploration and the discovery of connections in information.
    • LO 3.2.1 - Extract information from data to discover and explain connections or trends. [P1]
  • EU 3.3 - There are trade-offs when representing information as digital data.
    • LO 3.3.1 - Analyze how data representation, storage, security, and transmission of data involve computational manipulation of information. [P4]
Big Idea - Algorithms
  • EU 4.1 - Algorithms are precise sequences of instructions for processes that can be executed by a computer and are implemented using programming languages.
    • LO 4.1.1 - Develop an algorithm for implementation in a program. [P2]
      • EK 4.1.1A - Sequencing, selection, and iteration are building blocks of algorithms.
      • EK 4.1.1B - Sequencing is the application of each step of an algorithm in the order in which the statements are given.
      • EK 4.1.1C - Selection uses a Boolean condition to determine which of two parts of an algorithm is used.
      • EK 4.1.1D - Iteration is the repetition of part of an algorithm until a condition is met or for a specified number of times.
      • EK 4.1.1E - Algorithms can be combined to make new algorithms.
      • EK 4.1.1F - Using existing correct algorithms as building blocks for constructing a new algorithm helps ensure the new algorithm is correct.
      • EK 4.1.1H - Different algorithms can be developed to solve the same problem.
      • EK 4.1.1I - Developing a new algorithm to solve a problem can yield insight into the problem.
    • LO 4.1.2 - Express an algorithm in a language. [P5]
      • EK 4.1.2A - Languages for algorithms include natural language, pseudocode, and visual and textual programming languages.
      • EK 4.1.2B - Natural language and pseudocode describe algorithms so that humans can understand them.
      • EK 4.1.2C - Algorithms described in programming languages can be executed on a computer.
      • EK 4.1.2G - Every algorithm can be constructed using only sequencing, selection, and iteration.
      • EK 4.1.2I - Clarity and readability are important considerations when expressing an algorithm in a language.
Big Idea - Programming
  • EU 5.1 - Programs can be developed for creative expression, to satisfy personal curiosity, to create new knowledge, or to solve problems (to help people, organizations, or society).
    • LO 5.1.1 - Develop a program for creative expression, to satisfy personal curiosity, or to create new knowledge. [P2]
      • EK 5.1.1A - Programs are developed and used in a variety of ways by a wide range of people depending on the goals of the programmer.
      • EK 5.1.1B - Programs developed for creative expression, to satisfy personal curiosity, or to create new knowledge may have visual, audible, or tactile inputs and outputs.
      • EK 5.1.1C - Programs developed for creative expression, to satisfy personal curiosity, or to create new knowledge may be developed with different standards or methods than programs developed for widespread distribution.
    • LO 5.1.2 - Develop a correct program to solve problems. [P2]
      • EK 5.1.2A - An iterative process of program development helps in developing a correct program to solve problems.
      • EK 5.1.2B - Developing correct program components and then combining them helps in creating correct programs.
      • EK 5.1.2C - Incrementally adding tested program segments to correct working programs helps create large correct programs.
      • EK 5.1.2I - A programmer's knowledge and skill affects how a program is developed and how it is used to solve a problem.
      • EK 5.1.2J - A programmer designs, implements, tests, debugs, and maintains programs when solving problems.
  • EU 5.3 - Programming is facilitated by appropriate abstractions.
    • LO 5.3.1 - Use abstraction to manage complexity in programs. [P3]
      • EK 5.3.1A - Procedures are reusable programming abstractions.
      • EK 5.3.1B - A procedure is a named grouping of programming instructions.
      • EK 5.3.1C - Procedures reduce the complexity of writing and maintaining programs.
      • EK 5.3.1D - Procedures have names and may have parameters and return values.
      • EK 5.3.1G - Parameters provide different values as input to procedures when they are called in a program.
      • EK 5.3.1H - Data abstraction provides a means of separating behavior from implementation.
      • EK 5.3.1K - Lists and list operations, such as add, remove, and search, are common in many programs.
      • EK 5.3.1L - Using lists and procedures as abstractions in programming can result in programs that are easier to develop and maintain.
  • EU 5.4 - Programs are developed, maintained, and used by people for different purposes.
    • LO 5.4.1 - Evaluate the correctness of a program. [P4]
      • EK 5.4.1E - Locating and correcting errors in a program is called debugging the program.
      • EK 5.4.1F - Knowledge of what a program is supposed to do is required in order to find most program errors.
      • EK 5.4.1H - Visual displays (or different modalities) of program state can help in finding errors.
  • EU 5.5 - Programming uses mathematical and logical concepts.
    • LO 5.5.1 - Employ appropriate mathematical and logical concepts in programming. [P1]
      • EK 5.5.1A - Numbers and numerical concepts are fundamental to programming.
      • EK 5.5.1D - Mathematical expressions using arithmetic operators are part of most programming languages.
      • EK 5.5.1E - Logical concepts and Boolean algebra are fundamental to programming.

Key Concepts

The students must understand how to open and read from an input file using Python.

The students must understand how to declare and write to an output file using Python.

Essential Questions

  • How can computation be employed to help people process data and information to gain insight and knowledge?
  • How do people develop and test computer programs?

Teacher Resources

Student computer usage for this lesson is: required

Python for Everybody by Charles Severance, http://do1.dr-chuck.com/pythonlearn/EN_us/pythonlearn.pdf.

Explanation of the CountIf function in Excel https://support.office.com/en-us/article/COUNTIF-function-e0de10c6-f885-4e71-abb4-1f464816df34.

The mbox.txt and mbox-short.txt files are in the Lesson Resources Folder.


Lesson Plan

Session 1

Getting Started (10 min)


  • List some pros and cons of inputting data for a program using only a keyboard.
  • List some pros and cons of displaying output of a program using only video display.

Have students review their journal entries as a class and note the advantages and disadvantages on a white board.

Guided Activity (40 min)

Before Starting The Activity:

  • Have the students open the book Python for Everybody by Charles Severance http://do1.dr-chuck.com/pythonlearn/EN_us/pythonlearn.pdf and navigate to Chapter 7 "Files" (page 79).
  • Students should also open PyCharm or the Runestone coding environment and create a new .py file. (Teachers may want to project PyCharm and the textbook on the board.)
  • The two files mbox.txt and mbox-short.txt are located in the Lesson Resources Folder. Students will need to have these saved in the same folder as their python file for the lesson.

Chapter 7: Files

The students should code the examples in the book as the teacher proceeds through the lessons.

  1. Section 7.1 Persistence
    1.  Emphasize the disadvantages of the loss of information when the computer is powered off and the frequent need to store data in a more permanent location.
    2. Introduce the concept of secondary memory.
  2. Section 7.2 Opening Files 
    1. After reading the brief text in Section 7.2, have students type the sample code to open the mbox.txt file and run it.
    2. Have students try to open another nonexistent file and see what kind of error they get.
    3. Check the student’s code for understanding:
      • Were the students able to open the mbox.txt file successfully?
      • Did the students get a “No such file or directory” error message when they attempted to open a nonexistent file?
  3. Section 7.3 Text Files and Lines
    1. Introduce the newline special character “end of line” which breaks the file into lines. In Python the newline is represented by “\n” in string constants.
    2. Have the students type in the sample code in 7.3 (page 81), which demonstrates the newline character’s function.
    3. Check the student’s code for understanding  
      • Did the “\n” cause the string to be displayed on two lines?
  4. Section 7.4 Reading Files
    1. Introduce the notion of reading each line in a file by using a while loop. (The students should already be familiar with for and while loops, but may need a quick review of the syntax.)
    2. Have students count the lines in the mbox.txt file by running the first sample code in 7.4 (page 82).
    3. Check the students’ code for understanding.
      • Does your code give the same output as in the book?
  5. Section 7.8 Writing Files
    1. Show the students how to open a file for writing by opening it with mode 'w' as a second parameter.
    2. After reading the brief text in Section 7.8, have students type the sample code to write to the output.txt file and run it. Remind them to use the newline character, “\n”, at the end of each line.
    3. Check the student’s code for understanding:
      • Were the students able to write to the output.txt file successfully?
      • Did the students remember to close their output file when finished? 

Session 2

Python Lab (50 min) 


Part 1:

  • Open the Python program(s) for rolling two six sided and one twelve-sided dice (students should have saved this from lesson 4-3).
  • Edit the code so it rolls the dice 1000 times and write the results of each roll to an output file named diceRolls.dat.

Part 2:

  • Import the diceRolls.dat file into your spreadsheet software (such as Excel) either as text or cvs file type.
  • Make use of the countif function to compare the distribution of the rolls for how many times each number 2 through 12 was rolled with the pair of six-sided dice to the distribution for the 12-sided die.
  • Show the comparison visually with an appropriate chart in your spreadsheet software.


Options for Differentiated Instruction

Have students work in pairs as the new concepts are introduced and practiced.

For a class needing more scaffolding: Work as a group. Have students take turns around the room to read aloud the brief text in each section in Chapter 7. Do the short exercises together with a "row captain" assigned to each row (or group) in the classroom who is in charge of checking that everybody in their row has completed each short task and has gotten the help needed to finish. Row captains help each other until the entire class has successfully completed each task. Report out on what challenges were encountered, recording problems and solutions at the front of the classroom as the class works. Rotate the role of row captain for each section.

For more independent students: Introduce/demonstrate the key ideas first and then allow student to work through Chapter 7 at their own pace.

Evidence of Learning

Formative Assessment

The teacher will check the student’s code for understanding.

The teacher will check for understanding as each new concept is introduced.


Summative Assessment

Exercise 7.1 Write a program to read through a file and print the contents of the file (line by line) all in upper case. Executing the program will look as follows:

python shout.py

Enter a file name: mbox-short.txt





SAT, 05 JAN 2008 09:14:16 -0500 

You can download the sample input file from https://www.py4e.com/code3/mbox-short.txt

Exercise 7.2 Write a program to prompt for a file name, and then read through the file and look for lines of the form:

X-DSPAM-Confidence: 0.8475

When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the line to extract the floating point number on the line. Count these lines and the compute the total of the spam confidence values from these lines. When you reach the end of the file, print out the average spam confidence.

Enter the file name: mbox.txt

Average spam confidence: 0.894128046745

Enter the file name: mbox-short.txt

Average spam confidence: 0.750718518519


Test your file on the mbox.txt and mbox-short.txt files.