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

Writing files

All topicsPractise exam questions
Knowledge organiser

File Handling Techniques

301.15c

Append or overwrite content.

What you need to know

Use .write() to save text to a file. Remember to add \n for new lines — write() does not add them automatically. Use "w" to start fresh or "a" to add to existing content.

Key points

  • .write(text) saves text to the file
  • Does NOT auto-add newlines — use \n explicitly
  • "w" mode creates/overwrites; "a" mode appends
  • Write strings only — cast numbers with str() first

Code examples

Writing to a file
python
scores = [85, 92, 78]
with open("scores.txt", "w") as f:
    for s in scores:
        f.write(str(s) + "\n")