File Handling (OCR GCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

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

file = open("fruit.txt")

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

Close

file.close()

file.close()

Read line

file.readline()

file.readline()

Write line

file.writeline("Oranges")

file.write("Oranges")

End of file

file.endOfFile()

endOfFile = False

Create a new file

newFile("Shopping.txt")

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

Append a file

n/a

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

Python example (reading data)

Employees

Text file

file = open("employees.txt", "r") # open file in read mode
endOfFile = False # set end of file to false
while not endOfFile: # while not end of file
  name = file.readline() # read line 1
  department = file.readline() # read line 2
  salary = file.readline() # read line 3
  age = file.readline() # read line 4

  print("Name: ", name) # print name
  print("Department: ", department) # print department
  print("Salaray: ", salary) # print salary
  print("age: ", age) # print age
  
  if name == "": # if name is empty
    endOfFile = True # set end of file to true

file.close() # close file

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

Python example (writing new data)

Employees

 

file = open("employees.txt", "a") # open file in append mode
file.write("Polly\n") # write line (\n for new line)
file.write("Sales\n")
file.write("26000\n")
file.write("32\n")

file.close() # close file

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 for both

    year = input("Enter year")

  • file = open("books.txt") 1 mark

  • file.writeline(title) 1 mark for both

file.writeline(year)

  • file.close() 1 mark

Last updated:

You've read 0 of your 10 free revision notes

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?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

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.