AQA Computer Science GCSE
These pages were copied over from the old syllabus and will get updated as I work through the new one. That means they won't always be totally up to date and I might miss something - check the updated date at the bottom of the page.
Programming Concepts - File Handling
When a program is running, data is stored in variables. When the program stops, the data is lost.
To keep data when a program is closed you need to save it in a text file. To do this you need to know how to open, read, write and close files. This process is known as file handling.
Note that file handling is a slightly complex process and a whole set of problems can occur. The good news is that it's very unlikely that you would need to write pseudocode for file handling in an exam.
File Handling theory - with pseudocode
Text file
File read/write in Python uses text files. To follow the examples below you'll need the highscores text file below.
Highscores text file - right click and Save As
You need to make sure you save the highscores.txt file in the same folder as your Python files. This is crucial. If the files are saved somewhere else then the programs won't work!
Reading a file
Read File theory notes from class
To read from a file in Python it first needs to be opened, using the switch "r" to open it in read mode. The file is the read and the data in it stored in a variable - in the example I use a variable called scores. Then you close the file.
myFile = open("highscores.txt", "r")
scores = myFile.read() # assign data in file to variable
myFile.close()
print(scores)
Note that the data in the text file is stored as a string at this stage. There are more complex ways to save data using arrays (lists), but we'll keep it simple to begin with.
It is really important to make sure that the text file is closed at the end of the process. All sorts of problems can occur if the file isn't closed properly, so really take care with this.
You can also try using the readline() command. Experiment with this and see if you can figure out how to use it:
scores = myFile.readline()
Writing to a file
Write File theory notes from class
To write to a file in Python it first needs to be opened, this time using the switch "w" to open it in write mode.
dataToWrite = "Sally Shark, 120"
myFile = open("highscores.txt", "w")
myFile.write(dataToWrite)
myFile.close()
To see the new contents of the text file you either need to run your readfile program
Solving the Write problem
When the file is written it will overwrite the existing file, replacing it with just the new text (in the example, "Sally Shark, 120").
To solve this problem the technique to use is:
- open the file and read the contents in
- add the new data to the contents using concatenation
- open the file again and write the new, combined data to it
- open the file again to read the content in and print them out
This process can be implemented using this code:
myFile = open("highscores.txt", "r")
scores = myFile.read()
myFile.close()
# add the new data to the scores variable
scores = scores + "Sally Shark, 120"
# write the data to the file
myFile = open("highscores.txt", "w")
myFile.write(scores)
myFile.close()
# read the file in and print it to check contents
myFile = open("highscores.txt", "r")
newscores = myFile.read()
myFile.close()
print(newscores) # print the scores
The code does something odd to the text file. To solve this problem you can replace the line:
scores = scores + "Sally Shark, 120"
with:
scores = scores + "\nSally Shark, 120"
You should be able to figure out what the \n switch does.
You can use \n in a print statement as well if you want to:
print("What is the best way\nto split text over\nmore than one line?")
Advanced Read/Write Technique
Go the the Advanced Read/Write page to work on developing a more advanced way to deal with read/write file handling in a program.