![]() |
|
CHAPTER 16 — File Handling: Reading & Writing Data - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 16 — File Handling: Reading & Writing Data (/showthread.php?tid=234) |
CHAPTER 16 — File Handling: Reading & Writing Data - Leejohnston - 11-15-2025 Chapter 16 — File Handling: Reading & Writing Data This chapter teaches how Python reads and saves real files — unlocking data storage, logs, documents, and real applications. Programs become powerful when they interact with files: • save notes • keep scores • store settings • load data • create logs • write reports Python makes this simple using the built-in open() function. --- 16.1 Opening a File Format: Code: open(filename, mode)Common modes: Code: "r" read only--- 16.2 Reading a File Example file: notes.txt Code: with open("notes.txt", "r") as file:with automatically closes the file — very important. --- 16.3 Reading Line by Line Code: with open("data.txt", "r") as file:.strip() removes the newline at the end. --- 16.4 Writing to a File "w" overwrites the file: Code: with open("output.txt", "w") as file:If the file doesn't exist, Python creates it. --- 16.5 Appending to a File "a" adds content without deleting what's already inside. Code: with open("log.txt", "a") as file:--- 16.6 Writing Multiple Lines Code: lines = ["First line\n", "Second line\n", "Third line\n"]--- 16.7 Checking if a File Exists Code: import os--- 16.8 Reading Data and Converting Types Example file: 5 12 27 Reading as numbers: Code: numbers = []--- 16.9 Saving Structured Data (Simple Format) Saving: Code: with open("profile.txt", "w") as f:Reading: Code: profile = {}Now: Code: profile["name"]--- 16.10 Using JSON for Data Storage Perfect for storing dictionaries. Code: import jsonLoading: Code: with open("save.json", "r") as f:--- 16.11 Mini Project — Simple Notes App Write a program: 1. Ask the user to type a note 2. Append it to notes.txt 3. Confirm “Note saved.” Each new note goes on a new line. --- 16.12 Challenge — High Score System Requirements: • File: highscores.txt • Ask for a player name and score • Save them in the format: Code: Mia: 42• After saving, load the whole file • Display all scores sorted by score BONUS: Find the highest scorer and display their name. --- 16.13 Chapter Summary • open() controls reading/writing • "w" overwrites, "a" appends • use with open(...) to avoid errors • .read(), .readline(), loops for reading • JSON is excellent for structured data • file handling allows real persistent programs Next: Chapter 17 — Errors & Exception Handling This is where beginners learn how to make programs crash-proof, safe, and professional. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |