Sometimes you will need to write data to a file.
The process of writing to a file is always the same. The only thing that changes is what file you are writing to and what you write to the file.
Below are the key steps to the process:
- Open a connection to a file in append mode.
- Write the information you want to a file
- Close the connection to the file
Write modes
When you read from a file you would use the cost file = open("filename.csv","r")
. The r
stands for read. It would make sense that when you write to a file you use w
instead, but you don’t. w
does stand for write mode, but it means overwrite. Therefore each time you write to the file it will overwrite what is in there already.
Therefore when you want to open a connection to a file to write to, you use append
or a
mode. Append means add. It will add to the file rather than overwrite what is already there.
NOTE: you can call the variable writefile whatever you want.
Worked example - File Writing - Christmas List
Imagine a program that when run asks the user to enter a gift they want for Christmas, how much it costs and the priority of how much they want it.
Step 1 - Getting the information to write to the file
Before you can write to the file you need to get the information from the user that you want to add to the file.
Step 2 - Creating a connection to the file
This code will create a connection to the file filename.csv
in a
mode which stands for append. It can then be referred to as the variable name which is writefile
Step 3 - Write the information to the file
As you are writing to a csv
file you need to separate each piece of data that you want to add with a comma.
NOTE: the at the end means new line, this means next time you write to the file it will add the new set of information on a new line.
Step 4 - Closing the connection
Once you have written to the file you need to close the connection to the file. This means it can be used by other parts of the program if needed.
The final code looks like this:
The finished program looks like this when run:
The file created looks like this:
Example program 1
Deciding whether to write to a file
Sometimes before you add information to a file, you might need to check if it meets certain requirements. This will require building selection (IF) into your code.
At the moment the program above will always write the information entered to a file. Consider this new requirement:
Santa is running out of money, it should only write to the file gifts that cost less than £150, otherwise say they cost too much.
How to do this:
After the information has been entered create an if
that checks if the if
the cost is less than £150.
if
the cost is less than £150, then the lines of code that write to a file should be run.
if
the cost is not less than £150, add an else
and add code to say that the gift is too expensive.
The final code now looks like this:
Example program 2
Checking if data exists in a file before adding new data
Sometimes you will need to create a program that reads through a file to see if certain information exists. If it doesn’t then write it to a file.
This program asks the user to enter their names, email address and the number of years they have been teaching. It then looks in the file to see if the email address already exists. If it doesn’t it will write the information entered to a file, if it does it will say that person already exists.
The CSV file looks like this:
When run in IDLE:
Example Program 3
Reading data from one file and writing to a separate file
This program looks through a file that contains a list of students and the scores that they have got in four tests. When run it should calculate the average score for each student. If it is over 50 it should write their name and their average score to a new file, if not it should do nothing. The program should also count how many people have averaged above 50.
The CSV file looks like this:
The program when run: