That Blue Square Thing

AQA Computer Science GCSE

February 2019: this area of the site is being built just now. There are areas where there is no content yet. That will get added over the next 6 months or so.

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.

PDF iconFile 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.

Text file iconHighscores 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

PDF iconRead 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.

# readfile program
myFile = open("highscores.txt", "r")
scores = # assign data in file to variable

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

PDF iconWrite 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.

# writefile program
dataToWrite = "Sally Shark, 120"

myFile = open("highscores.txt", "w")

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:

  1. open the file and read the contents in
  2. add the new data to the contents using concatenation
  3. open the file again and write the new, combined data to it
  4. open the file again to read the content in and print them out

This process can be implemented using this code:

# read the file in and store it in variable called scores
myFile = open("highscores.txt", "r")
scores =

# add the new data to the scores variable
scores = scores + "Sally Shark, 120"

# write the data to the file
myFile = open("highscores.txt", "w")

# read the file in and print it to check contents
myFile = open("highscores.txt", "r")
newscores =
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"


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.