Programming constructs - CCEAFile handling

Computer programs use data stores to organise the different types of data they contain. Data stores can be either a constant, variable or an array, and contain inputs, outputs, functions, instructions, sequences and more.

Part ofDigital Technology (CCEA)Digital development concepts (programming)

File handling

When we create and use , , constants and tuples in our programming, the values held in these data stores are held in RAM (Random Access Memory).

As RAM is , data will be lost when the computer is turned off. If we want to access the data from our programs, we will have to write the data to a stored on .

Reading from text files:

PseudocodeExplanation
Reading one line
myFile = open("data.txt", "r")
line = myFile.readLine()
myFile.close()
Opens the file data called 'data.txt' and stores the contents in a variable 'myFile'. Reads the first line and stores in a variable called 'line'. Closes the file.
Reading all the lines
myFile = open("data.txt", "r")
lines = myFile.readLines()
myFile.close()
Opens the file data and stores the contents in a variable 'myFile'. Reads all the lines in the file and stores in a variable called 'lines'. Closes the file.
Alternatively
with open("data.txt", "r") as
myFile: for line in myFile: print(line)
Using a 'with loop', only keeps the file open for the duration of the loop. This will prevent the file being locked, if required later.
Reading one line
Pseudocode
myFile = open("data.txt", "r")
line = myFile.readLine()
myFile.close()
ExplanationOpens the file data called 'data.txt' and stores the contents in a variable 'myFile'. Reads the first line and stores in a variable called 'line'. Closes the file.
Reading all the lines
Pseudocode
myFile = open("data.txt", "r")
lines = myFile.readLines()
myFile.close()
ExplanationOpens the file data and stores the contents in a variable 'myFile'. Reads all the lines in the file and stores in a variable called 'lines'. Closes the file.
Alternatively
Pseudocode
with open("data.txt", "r") as
myFile: for line in myFile: print(line)
ExplanationUsing a 'with loop', only keeps the file open for the duration of the loop. This will prevent the file being locked, if required later.

Writing to text files:

PseudocodeExplanation
Writing a line to a file
myFile = open("data.txt", "w")
myFile.write("Hello World")
myFile.close()
The file data is open and stored in the variable 'myFile'. The string "Hello World" is written to the file. The file is closed.
Alternatively
with open ("data.txt", "w") as
myFile: data = 'Once upon a time...' myFile.write(data)
File is open in write mode using a 'with loop'. A string is assigned to a variable called 'data'. The variable is written to the file. The file does not need to be closed.
Writing a line to a file
Pseudocode
myFile = open("data.txt", "w")
myFile.write("Hello World")
myFile.close()
ExplanationThe file data is open and stored in the variable 'myFile'. The string "Hello World" is written to the file. The file is closed.
Alternatively
Pseudocode
with open ("data.txt", "w") as
myFile: data = 'Once upon a time...' myFile.write(data)
ExplanationFile is open in write mode using a 'with loop'. A string is assigned to a variable called 'data'. The variable is written to the file. The file does not need to be closed.

There are three modes in which files can be opened. Read mode will allow you to read from a file, write mode will allow you to write to the file, and append mode will allow you to append the file (add to the end of it).