That Blue Square Thing

Computer Science GCSE

Read/Write Files

You need to be able to read a text file in to a program, update it and then write it back to a text file. This will allow files to be saved so that you can use them again at a later date.

Reading and writing to files is slightly tricky, although probably not as hard as it might appear when you read books on it! If you have a text file separated with data in line and the items separated by commas, like the file below, then it's really easy to read in data at least.

Text file iconSongs text file - save this as a plain text file called readinfile.txt. Make sure you know where you saved it!

Reading in a Text File

The strategy here is straight forward. If you open the text file you'll see that it's organised in rows. That means we can:

  1. create an empty list to store the contents of the text file in (I'll call it songList)
  2. read in the first line of text and store it as a list
  3. add this list to songList
  4. keep reading in new lines of text, storing them as lists and adding them to songList
  5. when we get to the end of the text file stop

It is possible to code that process line by line, but there's a quicker way using a module called csv. This won't work with every kind of text file you might want to use, but is a good place to start.

Here's some code.

# make sure to save this in the same folder as the text file

import csv # import csv module

with open("readinfile.txt", newline="") as inputfile:
songs = list(csv.reader(inputfile))

print(songs) # print list all in one go
print()

for i in range(0, len(songs)): # print one line at a time
print(songs[i])

That's a nice simple way of reading in a file in about three lines of code. Not always what you want to do but a good starting point!.

Using Text Data

So, there's a list of songs. Say I want just the songs which are Dance tunes (ones with DA next to them), how do I do that?

Here's a strategy:

  1. read in the whole list into a list of lists - as above
  2. set up an empty list called danceSongs to store the DA tunes on
  3. use a for loop to look at each song in turn - passing each song into a temporary variable called currentSong
  4. look at the second element in currentSong (index 1 remember)
  5. if this element is "DA" then add the song to the list of Dance tracks
  6. otherwise do nothing

Let's see how this would work:

# code to pick out just dance tracks

import csv # import csv module

with open("readinfile.txt", newline="") as inputfile:
songs = list(csv.reader(inputfile))

danceSongs = [ ]

for i in range(0, len(songs)): # look at one line at a time
currentSong = songs[i]
if currentSong[1] == "DA":
danceSongs.append(currentSong) # add to the list

for i in range(0, len(danceSongs)): # print one line at a time
print(danceSongs[i])

Once you can do this you can look through lists of lists without too much trouble. The ley is to think about writing to a new list each time you want to find something specific. You can then iterate over that list to pull out the data you actually need.

Writing Text Files

This means saving text files so that you can use them again.

This section is going to have to wait - it's not essential just now...