OCR GCSE Computer Science June 2023 Paper 2, Question 6(e): the SaveLogs procedure

OCR J277/02 June 2023, Question 6(e): write a procedure, SaveLogs, that takes a string and a filename as parameters and stores the string in the text file. Model answers in OCR reference language and Python, the six marks explained, and the procedure to run.

Past paper questionOCR J277/02June 2023 Paper 26 marksWrite a program

Question 6(e) of the OCR GCSE Computer Science Paper 2 sat on 25 May 2023 (J277/02) is worth 6 marks for about five lines. It joins two things: defining a procedure with parameters, and writing to a text file. File handling always follows the same three steps, open, write, close, and each one is a mark.

We do not copy the exam paper here. Open it beside this page: OCR June 2023 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

Write a procedure called SaveLogs() that:

  • takes the string of data to be stored as a parameter;
  • takes the filename of the text file as a parameter;
  • stores the string of data in the text file.

It is in Section B, so it must be in OCR Exam Reference Language or a high-level language.

A model answer

In OCR Exam Reference Language:

procedure SaveLogs(data, filename)
    logFile = open(filename)
    logFile.writeLine(data)
    logFile.close()
endprocedure

In Python:

def SaveLogs(data, filename):
    logFile = open(filename, "w")
    logFile.write(data)
    logFile.close()

In Python the "w" matters: open(filename) on its own opens the file for reading. "a" (append) is accepted too, and for a log it is the better choice, because "w" wipes what was there before.

Where the six marks are

There are seven points, and any six get full marks:

  • defining a procedure called SaveLogs;
  • giving it two parameters;
  • opening a file;
  • using the filename parameter to open it;
  • writing to the file;
  • writing the data parameter;
  • closing the file.

Where the marks are lost

  • Putting the filename in quotes. open("filename") opens a file that is literally called filename. The parameter is a variable, so it has no quotes. The same goes for write("data").
  • Overwriting the parameters. filename = input() inside the procedure throws away what was passed in. The mark scheme withholds the parameter mark for that.
  • Calling and not defining. SaveLogs("x", "log.txt") on its own is a call. The question says write the procedure, which means the procedure or def line and the body.
  • Forgetting to close. It is the easiest mark here. "Close file" is enough in pseudocode.
  • Code outside the procedure. Opening the file before the def line loses the marks for using the parameters.

Run it

The procedure in Python, called twice. Then the program reads the file back to show what was stored. Because this version appends, both lines are there.

It saves two log lines to log.txt and reads them back. Change the a to a w and only the second line survives.
The program
from bugbot import *
connect()

def SaveLogs(data, filename):
    logFile = open(filename, "a")
    logFile.write(data + "\n")
    logFile.close()

SaveLogs("05/02/2023,WS2,Window,38", "log.txt")
SaveLogs("05/02/2023,MS1,Motion,2", "log.txt")

logFile = open("log.txt", "r")
print(logFile.read())
logFile.close()
led("green")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to OCR J277 June 2023 Paper 2 Question 6(e)?

Define procedure SaveLogs(data, filename). Inside it, open the file using filename, write data to it with writeLine, and close the file. Then end the procedure.

How do you write to a file in OCR Exam Reference Language?

Open it with myFile = open("name.txt"), write a line with myFile.writeLine(text), and close it with myFile.close(). To make a new file first, use newFile("name.txt").

What is the difference between a procedure and a function?

Both are subprograms that can take parameters. A function returns a value to the code that called it. A procedure does not.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F7.1 Reading and writing files Files and databases
  2. F4.1 Writing functions Functions and structured code
  3. F4.2 Parameters and return values Functions and structured code
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.