Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / File Handling Techniques

Opening files

All topicsPractise exam questions
Knowledge organiser

File Handling Techniques

301.15a

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.

Key points

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