File Handling
Reading and writing data to and from a file is an important process for most programs - where it is required. For GCSE we only need to concern ourselves with text files, that is files that contain raw ASCII data. Many programs will also save and read files in different formats loosely termed binary files. These will not be considered here.
The process is not that complicated:
- obtain a handle to the file
- process the file by reading its contents into a variable, or writing the contents of a variable to the file
- close the file
A text file consists of a sequence of lines of text in the file e.g.
Or, it might be a series of records.
Records
The record has fallen out of favour in preference for other ways of organising data e.g. lists, or tuples but it does appear in the AQA syllabus. So for completeness, a record is a data structure that contains a number fields, each field can be a different data type.
Example
To clarify, Python does not have the concept of a record. It does not need one as other data structures such as dictionary that work just as well if not better:
The collections
library also has a namedtuple
data structure:
We could (though it's not recommended) store the same data as a list:
Reading from a text file
Assume we have a file of students, students.txt
:
Each line in the file is a record, typically several fields separated by commas. Each row represents one student. This structure is also known as a comma separated value file, or CSV.
Obtain a handle to the file
Typically, when we open a file we will be opening to perform one type of action, or mode:
- write: allows writing to the file, usually overwriting any existing content
- append: adds data to the end of the file
- read: allows reading from the file
When we open the file we get back a file handle, essentially we're asking the operating system to grant us access to that file. The OS returns a unique identifier, commonly known as the file handle. It's a special key that allows us to interact with the file. But, it is not the file itself!
Process the file contents
We want to read the contents of the file, the contents need to be put somewhere, i.e. assigned to a variable. We might want all the lines in the file, or we might want to process each line one at a time.
Example:
Writing to a text file
The following should be straightforward:
Take particular note of the \n
at the end of the string in line 6. This is the new line character and ensures any subsequent record is written to the next line of the file.
Closing the file
In the previous examples we used student_file.close()
. It is important to close any file when it is no longer needed:
- Frees up memory
- File cannot be processed when it is closed
AQA Pseudocode
There are no file handling functions in the AQA pseudocode.
OCR Pseudocode
The following functions are specified as part of the OCR pseudocode:
Function | Description | Example |
---|---|---|
openRead(file) |
Open a file to read | file = openRead('students.txt') |
readLine() |
Read a line from the file | line = file.readLine() |
endOfFile() |
Determines when the end of the file has been reached | while not file.endOfFile() |
openWrite(file) |
Open a file for writing | file = openWrite('students.txt') |
writeLine() |
Write a line to the file | file.writeLine(line) |
close() |
Close the file | file.close() |