That Blue Square Thing

AQA Computer Science GCSE

September 2020: This page is the for the 2022 exam version of the AQA Computer Science GCSE course. This is for Year 10 teaching in September 2020.
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 with Lists

The read/write programs methods described on the File read/write and Advanced read/write pages cope perfectly well when you want to read in a simple text file. But they aren't great when you want to create an array (or list) of items.

You might want to create an array of data from your text file for a number of reasons. It's often an easier way to work through a list of items and use the data associated with them.

Download and save a copy of the countries text file to work through these exercises. Remember, the text file needs to be saved in the same folder as your program.

Text file iconCountries 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!

Using Readline

The basic read/write techniques create a set of text which will print over multiple lines. It looks great when you print it - but it's difficult to do very much with it other than add another line at the bottom.

To do things with the items inside the text file it's easiest to read the data in line by line. That's where readline() comes in.

# readfile program using readline
countryFile = open("countries.txt", "r")
aCountry = countryFile.readline() # read in one line

This will read one country only, which isn't all that useful. To read in multiple countries we can use readlines() instead.

# readfile program using readlines
# this reads in all items and creates a list
countryFile = open("countries.txt", "r")
countryList = countryFile.readlines() # read in all lines

This will create a list of items, but there's a problem - each item has \n at the end of it. Which sort of spoils the way it looks:

['United Kingdom\n', 'France\n', 'Belgium\n', 'United States\n', 'Canada\n', 'Italy\n', 'Spain\n', 'Netherlands\n', 'Germany\n', 'Denmark\n', 'Sweden\n', 'Norway\n', 'Switzerland\n', 'Vatican City\n', 'Monaco']

The \n creates a new line in the text file, so when you use the simple read/write methods and print the data you've read in the list looks nice. The problem is that the \n gets added to the end of each item if you add it to a list - it's a "non-printable character" but the information (when to create a new line) still needs to be included.

To use the list of countries properly, however, we really want to remove the \n.

Removing the Line Breaks

To remove the line breaks (the \n) we'd like to be able to work through the list one item at a time and get rid of the \n. That's not quite as easy as replacing each item in the list, so it's easier to go back one stage at start again using readline.

The key idea here is that it's best to create a new list first to add each item to as you go through.

# read in a list and remove \n
countryList = [] # set up an empty list

countryFile = open("countries.txt", "r")
for country in countryFile: # work through line by line
country = country.rstrip("\r\n") # remove the \n
countryList.append(country) # add the country to the list


If you want to read in the list of country names as a set of individual lists (and there are good reasons why you might want to...), there's an easy way to adapt this code shown on the Read/Write Lists page in the Programming Project unit.