{ File I/O Exercises. }

Part 1 - Text Files

For the first part of this exercise assume you have a file called students.txt which simply contains a bunch of student names, one name per line. Your goal is to write two functions:

  • add_student - accepts a parameter of first_name and writes to a file called students.txt.
  • find_student - accepts a parameter of first_name and returns the first student found

Bonuses

  1. Write the following additional functions:
    • update_student - accepts a parameter of first_name and new_name and updates first student found
    • remove_student - accepts a parameter of first_name and removes the student from the text file
  2. Add a unique id for each student so that you can find a student by their id instead of first name (which breaks if you have the same first name for multiple students)

Part 2 - CSV

For the next part of this exercise, you will be working with CSVs, so first create a file called users.csv and then work on the following two functions:

  • one that prints out all of the first and last names in the users.csv file
  • one that prompts us to enter a first and last name and adds it to the users.csv file.

For solutions to these exercises, click here.

When you're ready, move on to Generators and Iterators

Continue

Creative Commons License