09 Checkpoint: Text Files
Purpose
Check your understanding of text files and lists by writing a program that reads the contents of a text file into a list and then changes some of the values in the list.
Helpful Documentation
- The Reading Files article explains how to setup VS Code so that your Python program can read from a text file.
- The Text Files section of the preparation content for this lesson contains example code that shows how to read a text file into a list.
- The Lists section of the preparation content for lesson 7 contains example code that shows how to replace and remove elements in a list.
- The list.count method counts and returns the number of times a value appears in a list.
Assignment
You must do the following to setup VS Code so that your program can read from a text file:
- Download the
provinces.txt
file and save it in the same folder where you will save your Python program. - Using VS Code, open the folder where you saved the
provinces.txt
file by doing the following:- On a computer running the Mac OSX operating system:
- Click the "File" menu, then "Open Folder..."
- Find and select the folder where you saved the
provinces.txt
file. - Click the "Open" button.
- On a computer running the Windows operating system:
- Click the "File" menu, then "Open Folder..."
- Find and select the folder where you saved the
provinces.txt
file. - Click the "Select Folder" button.
Now that you have correctly setup VS Code so that your program
can read the provinces.txt
file, open that file in VS
Code and examine it. Notice that the file contains a list of names
of Canadian Provinces. Notice also that the file contains "AB"
several times which is an abbreviation for Alberta.
Write a Python program named provinces.py
that reads
the contents of provinces.txt
into a list and then
modifies the list. Your program must do the following:
- Open the
provinces.txt
file for reading. - Read the contents of the file into a list where each line of text in the file is stored in a separate element in the list.
- Print the entire list.
- Remove the first element from the list.
- Remove the last element from the list.
- Replace all occurrences of "AB" in the list with "Alberta".
- Count the number of elements that are "Alberta" and print that number.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Run your program and ensure that your program’s output matches the output below. (Your program may wrap the province names differently.)
> python provinces.py ['Ontario', 'Prince Edward Island', 'Ontario', 'Quebec', 'Saskatchewan', 'Alberta', 'Nova Scotia', 'Alberta', 'Northwest Territories', 'Saskatchewan', 'Nunavut', 'Nova Scotia', 'Prince Edward Island', 'Alberta', 'Nova Scotia', 'Nova Scotia', 'Prince Edward Island', 'British Columbia', 'Ontario', 'Ontario', 'Newfoundland and Labrador', 'Ontario', 'Ontario', 'Saskatchewan', 'Nova Scotia', 'Prince Edward Island', 'Saskatchewan', 'Ontario', 'Newfoundland and Labrador', 'Ontario', 'British Columbia', 'Manitoba', 'Ontario', 'Alberta', 'Saskatchewan', 'Ontario', 'Yukon', 'Ontario', 'New Brunswick', 'British Columbia', 'Manitoba', 'Yukon', 'British Columbia', 'Manitoba', 'Yukon', 'Newfoundland and Labrador', 'Ontario', 'Yukon', 'Ontario', 'Alberta', 'Nova Scotia', 'Newfoundland and Labrador', 'Yukon', 'Nunavut', 'Northwest Territories', 'Nunavut', 'Yukon', 'British Columbia', 'Ontario', 'Alberta', 'Saskatchewan', 'Prince Edward Island', 'Saskatchewan', 'Prince Edward Island', 'Alberta', 'Ontario', 'Alberta', 'Manitoba', 'Alberta', 'British Columbia'] Alberta occurs 9 times in the modified list.
Sample Solution
When your program is finished, view the sample solution for this assignment to compare your solution to that one. Before looking at the sample solution, you should work to complete this checkpoint program. However, if you have worked on it for at least an hour and are still having problems, feel free to use the sample solution to help you finish your program.
Submission
When complete, report your progress in the associated I‑Learn quiz.