⏲️ Est. time to complete: 15 min. ⏲️
As a user, I want to be able to have my to-do list automatically loaded from a file when I start the program.
- Existing to-do list items from the last time I ran the program are loaded into this program
- The existing to-do list items will be a ".txt" file
- each line in the file will contain a single to-do item.
- if there is no file, then the program should load an empty collection
The following resources/videos will help you get a better understanding of some of the basic Python concepts you will use to complete this user story.
- Working with Files (~ 1 min) - same video from previous user story
- Exception Handling (12:53 min)
- Exception Handling Demo (3:57 min)
In order to complete this user story, you will need to complete the following tasks:
click here if you are running in Codespaces
If you are using Codespaces, please go into the repo you created for this project and start the Codespace by directly clicking on the Codespace name. In the case of the image below, the Codespace is named symmetrical computing-machine. Each Codespace auto-generates a unique name for that instance so your Codespace name will be different.
This will take you directly into a online Visual Studio Code environment.
click here if you are running Visual Studio Code in a local development environment
-
From the terminal/console window, navigate to the project directory
cd <project directory>
-
Open up Visual Studio Code in the project directory by executing the following command.
code .
Open the app.py
file in the root of your project folder.
The first change we will make is to update the logic for the start of the program. We will add code to load the to-do list from a file when the program starts. Add the following code to the beginning of the program - right after the todo_list = []
statement:
Tip
You can ask GitHub Copilot to help here with this prompt load todo list from todo_list.txt file
try:
with open("todo_list.txt", "r") as file:
for line in file:
todo_list.append(line.strip())
except FileNotFoundError:
print("No saved items found")
This code will open a file named todo_list.txt
in the current directory and read each line from the file. The r
parameter in the open
function tells Python to open the file in read mode. The for
loop will iterate through each line in the file and add it to the todo_list
. The strip
function is used to remove any leading or trailing whitespace from the line before adding it to the todo_list
. If the file does not exist, the except FileNotFoundError
block will catch the error and the program will first print a message letting the user know it didn't find any previously saved items and then continue without loading any items. As an example, if you are running the program for the very first time, the file will not have been created so the exception would get thrown and we want the program to continue to execute.
Now let's see this application in action. Open the terminal and navigate to the folder where your app.py
file is located. Run the application by typing python app.py
and pressing the enter key or simply click the play button in the top-right corner of the Visual Studio Code window.
- When you start the program you should automatically see the list of to-do items previously saved in the file.
Note
📄For the full source code for this exercise, please see here
🔼 Home | ◀ Previous user story | Next user story (in next sprint) ▶