Unfortunately, when you want to update information in a file when programming you cannot simply find the information and say what you want to change, you have to recreate the whole file changing the single piece of information you want to update, these are the steps you must go through:

  1. Read through each line of the original file that contains the information you want to update.
  2. If the line of the file has the information you want to update, then you can change the information, you then write this information to an updated file
  3. If the line of the file doesn’t have the information you want to update, then you write this information (unchanged) to the updated file
  4. Once the entire original file has been checked, the updated file should have the same number of lines in, but with the updated information. Then you must delete the original file and rename the updated fileto the name of the original file.

NOTE: There are no new programming skills here, however you will need to combine the skills for reading and writing to a file together and program it in a logical order.

Worked example - File Updating - Staff Details

Imagine a program that has a file that contains information of staff working in a school. The program when run should ask the user to enter an email address to find the staff details. When it finds a match it should ask for the new information and then update the file. If it doesn’t find a match it should say that the email address is not in the file.

The CSV file looks like this:

Updating1

Program Summary A summary of how the program should work:

  • Read a line from the file and compare the email address
  • If it is a match ask the user to enter the new information and then write the new information to the update file.
  • If it isn’t a match write the information that has been read into the list to the update file unchanged.
  • After every line in the whole file has been checked, delete the original file, rename the updated file to the name of the original file.

Consider this code:

email = input("Enter the email address you are looking for:")
file = open("staff.csv","r")
found=False
for line in file:
    staff = line.split(",")
    if staff[2] == email:
        found=True
        print("Enter the new information for this member of staff")
        firstname = input("Enter their first name: ")
        surname = input("Enter their surname: ")
        email = input("Enter their email address: ")
        yearsteaching =input("Enter their number of years teaching: " )
if found==True:
    print("Details updated")
else:
    print("That staff member's email cannot be found in the file, no changes made")

So far this program will ask the user to enter an email address. It will then open the staff file and check each line in the file for that email address. If it finds it then it will ask the user to enter the new information, but not do anything with it.

After the loop has finished it will check to see if the found variable is True and if it is say details updated and if not say the staff member cannot be found.

NOTE: it doesn’t actually update the details at the moment.

The code once it updates the file:

import os, sys #new line 1
email = input("Enter the email address you are looking for:")
file = open("staff.csv","r")
found=False
for line in file:
    staff = line.split(",")
    writefile = open("staffupdated.csv","a") #new line 2
    if staff[2] == email:
        found=True
        print("Enter the new information for this member of staff")
        firstname = input("Enter their first name: ")
        surname = input("Enter their surname: ")
        email = input("Enter their email address: ")
        yearsteaching =input("Enter their number of years teaching: " )
        writefile.write(firstname + "," + surname + "," + email + "," + yearsteaching+"
") #new line 3 else: #new line 4 writefile.write(staff[0] + "," + staff[1] + "," + staff[2] + "," + staff[3]) #new line 5 writefile.close() #new line 6 file.close() #new line 7 os.remove("staff.csv") #new line 8 os.rename("staffupdated.csv","staff.csv") #new line 9 if found==True: print("Details updated") else: print("That staff member's email cannot be found in the file, no changes made")

What the new code does

  • New Line 1 - imports a library that is needed so that you can delete and rename files.
  • New Line 2 - opens a connection to a new temporary file called staffupdated.csv in append mode. This will be the new file with the updated information in.
  • New Line 3 - if the email address is found and the user has entered all the new information, it will write to the file all the new updated information.
  • New Line 4 - an else is added so that if a line of the file has been read and it isn’t a match it must write the information unchanged to the temporary file.
  • New Line 5 - it will write the information that has been read into the list to the temporary file unchanged.
  • New Line 6 - closes the connection to the file you are writing to. This should be done after the if but inside the for loop.
  • New Line 7 - closes the connection to the file you are reading from. This should be done after the forloop.
  • New Line 8 - this will remove the original file, in this case staff.csv
  • New Line 9 - this will rename the temporary file staffupdated.csv to staff.csv which will mean the updated information now appears in the original file.

When the program is run in IDLE and the staff member exists in the file:

Updating2

How the CSV file changes:

Updating3Before the update
Updating4After the update

When the program is run in IDLE and the staff member doesn’t exist in the file:

Updating5

Example Program 1 - Updating a File - Zoo Animals

This program looks through a file that contains a list of animals and how many of them there are in the zoo. When run it should ask the user the animal that they are looking for and if it finds it then ask for the new number of that animals at the zoo and update the file.

The CSV file looks like this:

Updating6

import os, sys #imports the os library
animal = input("Enter the animal you are looking for:") #asks the user to enter the animal they are looking for
file = open("zooanimals.csv","r") #opens the file in read mode
found=False #sets whether the animal is found to False
for line in file: #reads each line in the file
    details = line.split(",") #splits the line into the list called details
    writefile = open("zooanimalsupdated.csv","a") #opens the file zooanimalsupdated.csv in append mode
    if details[0] == animal: #checks if the animal is found in the list
        found=True #if it is changes found to True
        number = input("Enter the new number of " + animal + " at the Zoo:") #asks the user to enter the number of that animal at the zoo
        writefile.write(details[0] + "," + number+"
") #writes the animal and the new number to the temp file else: writefile.write(details[0] + "," + details[1]) #if that line doesn't contain the animal write the details from the list to the temp file unchanged. writefile.close() #closes the file that is being written to file.close() #closes the file that is being read os.remove("zooanimals.csv") #deletes the original file os.rename("zooanimalsupdated.csv","zooanimals.csv") #renames the temporary file to the original file name if found==True: #after the loop checks if the animal is found print("Details updated") #if the animal was found say details updated else: print("That animal cannot be found in the file, no changes made") #if the animal wasn't found say it cannot be found

The program when run in IDLE with an animal in the file:

Updating7

Updating8Updating9
Before UpdateAfter Update

The program when run in IDLE with an animal not in the file:

Updating10

Updating8Updating8
Before UpdateAfter Update