Skip to content

Latest commit

 

History

History
94 lines (62 loc) · 4.86 KB

File metadata and controls

94 lines (62 loc) · 4.86 KB

📖 Load To-Do items from a file - Step-by-Step

⏲️ Est. time to complete: 15 min. ⏲️

User Story

As a user, I want to be able to have my to-do list automatically loaded from a file when I start the program.

🎯Acceptance Criteria:

  • 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

🎓Know Before You Start

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.

📋Steps

In order to complete this user story, you will need to complete the following tasks:

Start your Codespace or Open Visual Studio Code locally

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.

Start Codespaces

This will take you directly into a online Visual Studio Code environment.

online visual studio code

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.


Updating the code base for new functionality

1. Update code to load the to-do list from a file on application start

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:

insertcode

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.


2. Run the application

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) ▶