File Handling (Cambridge (CIE) O Level Computer Science)

Revision Note

Flashcards
James Woodhouse

Expertise

Computer Science

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")

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

Exam Tip

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

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?

James Woodhouse

Author: James Woodhouse

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.