File Handling (Cambridge (CIE) O Level Computer Science): Revision Note

Exam code: 2210

James Woodhouse

Written by: James Woodhouse

Reviewed by: Lucy Kirkham

Updated on

File Handling

What is file handling?

  • File handling is the use of programming techniques to work with information stored in text files

  • Examples of file handing techniques are:

    • opening text files

    • reading text files

    • writing text files

    • closing text files

Concept

OCR exam reference

Python

Open

OPENFILE "fruit.txt" FOR READ

file = open("fruit.txt","r")

Close

CLOSEFILE "fruit.txt"

file.close()

Read line

READFILE "fruit.txt", LineOfText

file.readline()

Write line

OPENFILE "fruit.txt" FOR WRITE

WRITEFILE "fruit.txt", "Oranges"

file.write("Oranges")

Append a file

OPENFILE "fruit.txt" FOR APPEND

file = open("shopping.txt","a")

Pseudocode example (reading data)

Employees

Text file

OPENFILE "employees.txt" FOR READ

endOfFile ← FALSE  // Set end of file flag to false

WHILE NOT endOfFile DO
    READFILE "employees.txt", name  // Read line 1 (name)
    name ← TRIM(name)  // Remove extra spaces/newlines

    READFILE "employees.txt", department  // Read line 2 (department)
    department ← TRIM(department)  // Remove extra spaces/newlines

    READFILE "employees.txt", salary  // Read line 3 (salary)
    salary ← TRIM(salary)  // Remove extra spaces/newlines

    READFILE "employees.txt", age  // Read line 4 (age)
    age ← TRIM(age)  // Remove extra spaces/newlines

    IF name = "" THEN  // If name is empty (end of file)
        endOfFile ← TRUE  // Set end of file flag to true
    ELSE
        OUTPUT "Name: " & name  // Print name
        OUTPUT "Department: " & department  // Print department
        OUTPUT "Salary: " & salary  // Print salary
        OUTPUT "Age: " & age  // Print age
        OUTPUT ""  // Add a blank line for readability
    ENDIF
ENDWHILE

CLOSEFILE "employees.txt"

Greg
Sales
39000
43
Lucy
Human resources
26750
28
Jordan
Payroll
45000
31

Python example (reading data)

Employees

Text file

# Open file in read mode
file = open("employees.txt", "r")

endOfFile = False  # Set end of file flag to false

while not endOfFile:  # While not end of file
    name = file.readline().strip()  # Read line 1 and remove extra spaces/newlines
    department = file.readline().strip()  # Read line 2
    salary = file.readline().strip()  # Read line 3
    age = file.readline().strip()  # Read line 4

    if name == "":  # If name is empty (end of file)
        endOfFile = True  # Set end of file flag to true
    else:
        print("Name: ", name)  # Print name
        print("Department: ", department)  # Print department
        print("Salary: ", salary)  # Print salary (fixed typo)
        print("Age: ", age)  # Print age
        print()  # Add a blank line for readability

# Close the file
file.close()

Greg
Sales
39000
43
Lucy
Human resources
26750
28
Jordan
Payroll
45000
31

Pseudocode example (writing new data)

Employees

Text file

OPENFILE "employees.txt" FOR APPEND

// Write employee details to the file
WRITEFILE "employees.txt", "Polly"  // Name
WRITEFILE "employees.txt", NEWLINE
WRITEFILE "employees.txt", "Sales"  // Department
WRITEFILE "employees.txt", NEWLINE
WRITEFILE "employees.txt", "26000"  // Salary
WRITEFILE "employees.txt", NEWLINE
WRITEFILE "employees.txt", "32"  // Age
WRITEFILE "employees.txt", NEWLINE

CLOSEFILE "employees.txt"

Greg
Sales
39000
43
Lucy
Human resources
26750
28
Jordan
Payroll
45000
31

Polly
Sales
26000
32

Python example (writing new data)

Employees

 Text file

# Open file in append mode
file = open("employees.txt", "a")

# Write employee details to the file
file.write("Polly\n")        # Name
file.write("Sales\n")        # Department
file.write("26000\n")        # Salary
file.write("32\n")           # Age

# Close the file
file.close()

Greg
Sales
39000
43
Lucy
Human resources
26750
28
Jordan
Payroll
45000
31
Polly
Sales
26000
32

Examiner Tips and Tricks

When opening files it is really important to make sure you use the correct letter in the open command

  • "r" is for reading from a file only

  • "w" is for writing to a new file, if the file does not exist it will be created. If a file with the same name exists the contents will be overwritten

  • "a" is for writing to the end of an existing file only

Always make a backup of text files you are working with, one mistake and you can lose the contents!

Worked Example

Use pseudocode to write an algorithm that does the following :

  • Inputs the title and year of a book from the user.

  • Permanently stores the book title and year to the existing text file books.txt [4]

How to answer this question

  • Write two input statements (title and year of book)

  • Open the file

  • Write inputs to file

  • Close the file

Example answer

title = input("Enter title")
year = input("Enter year")
file = open("books.txt")
file.writeline(title)
file.writeline(year)
file.close()

Guidance

title = input("Enter title")

1 mark

year = input("Enter year")

file = open("books.txt")

1 mark

file.writeline(title)

1 mark

file.writeline(year)

file.close()

1 mark

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.

Lucy Kirkham

Reviewer: Lucy Kirkham

Expertise: Head of STEM

Lucy has been a passionate Maths teacher for over 12 years, teaching maths across the UK and abroad helping to engage, interest and develop confidence in the subject at all levels.Working as a Head of Department and then Director of Maths, Lucy has advised schools and academy trusts in both Scotland and the East Midlands, where her role was to support and coach teachers to improve Maths teaching for all.