Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Supplementary programming practice/File Handling Techniques

Opening files

301.15 File Handling Techniques

Practise exam questions

File Handling Techniques — Note 1 of 4

Open read write append.

What you need to know

Use open() with a mode to access a file. Common modes are "r" (read), "w" (write — overwrites), and "a" (append — adds to end). The with statement auto-closes the file.

What to know

  • "r" = read (default), "w" = write, "a" = append
  • "w" overwrites the entire file — data is lost!
  • "a" adds to the end without deleting existing content
  • with open() as f: auto-closes when done

Code examples

Opening files
python
# Read mode
with open("data.txt", "r") as f:
    contents = f.read()

# Write mode (overwrites)
with open("output.txt", "w") as f:
    f.write("Hello")

# Append mode (adds to end)
with open("log.txt", "a") as f:
    f.write("New entry\n")
← Previous topicOff-by-one301.14 Random Number GenerationNext note →Reading files301.15 File Handling Techniques

Back to File Handling Techniques

Specification notes for programming are in Paper 2.

File Handling Techniques

Note 1 of 4

  1. 1Opening files
  2. 2Reading files
  3. 3Writing files
  4. 4Closing files
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).