{"base":{"id":"cs11-05","subject":"Programming","title":"Reading and writing text files","scene":"mix","steps":[{"use":"cs11","mode":"file","eyebrow":"Writing a file","title":"Data that lasts","body":"Variables are lost when a program ends; files keep data. Write, append, then read.","actions":[{"id":"write","label":"Write"},{"id":"append","label":"Append"},{"id":"read","label":"Read a line"},{"id":"reset","label":"Reset"}]},{"use":"cs11","mode":"file","eyebrow":"Reading line by line","title":"Until end of file","body":"Read one line at a time until EOF.","actions":[{"id":"write","label":"Write"},{"id":"append","label":"Append"},{"id":"read","label":"Read a line"}]},{"use":"sys10","mode":"storage","eyebrow":"Where files live","title":"Secondary storage","body":"Files are saved on secondary storage such as SSDs, so they survive switching off.","actions":[{"id":"s0","label":"Magnetic"},{"id":"s1","label":"Solid state"},{"id":"s2","label":"Optical"}]},{"use":"cs11","mode":"quiz","look":"file","eyebrow":"Check yourself","title":"Modes","body":"A program must add new scores without deleting old ones.","question":"Which file mode?","options":["WRITE","APPEND","READ"],"answer":1,"right":"Correct.","wrong":"Not quite."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","items":[{"tag":"Write","title":"Pseudocode","body":"OPENFILE \"log.txt\" FOR WRITE\nWRITEFILE \"log.txt\", \"Started\"\nCLOSEFILE \"log.txt\"","code":true},{"tag":"Read","title":"Pseudocode","body":"OPENFILE \"log.txt\" FOR READ\nWHILE NOT EOF(\"log.txt\")\n    READFILE \"log.txt\", line\n    OUTPUT line\nENDWHILE\nCLOSEFILE \"log.txt\"","code":true},{"tag":"Python","title":"Write","body":"with open(\"scores.txt\", \"a\") as f:\n    f.write(\"Ana,72\\n\")","code":true},{"tag":"Python","title":"Read","body":"with open(\"scores.txt\") as f:\n    for line in f:\n        name, score = line.strip().split(\",\")","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Using WRITE instead of APPEND","body":"WRITE replaces the whole file."},{"tag":"Mistake","title":"Forgetting to close the file","body":"Data may not be saved; Python's with closes it for you."},{"tag":"Mistake","title":"Forgetting the newline","body":"Add \\n or lines will join together."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Data","title":"CSV files","body":"Comma-separated values files store tables as text and open in any spreadsheet program."},{"tag":"Systems","title":"Logs","body":"Servers write log files every second, recording what happened for troubleshooting."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one on paper before opening the card.","items":[{"tag":"Challenge","title":"Write","body":"Read scores.txt (name,score per line) and output the average score.\n\nAnswer: total and count inside the read loop; output total / count.","code":true},{"tag":"Challenge","title":"Explain","body":"Why does a program need files and not just variables?\n\nAnswer: variables are in RAM and are lost when the program closes; files persist on storage."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Try it yourself","items":[{"title":"High-score file","time":"25 minutes","need":["Python"],"steps":["Write a quiz that saves the player's name and score to a file.","Read the file and print all past scores."],"why":"Your program remembers data between runs.","safety":"None."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"Files store data…","options":["only in RAM","only while running","permanently on storage"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"WRITE mode…","options":["replaces existing contents","adds to the end","only reads"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"APPEND mode…","options":["deletes the file","adds to the end","reads lines"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"EOF stands for…","options":["edit or format","every other file","end of file"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"After using a file you should…","options":["close it","delete it","rename it"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"In Python, open(name, \"r\") opens for…","options":["writing","reading","appending"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"line.strip() removes…","options":["all letters","commas","spaces and newline characters at the ends"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"A CSV file separates values with…","options":["commas","tabs only","semicolons always"],"answer":0,"right":"Correct.","wrong":"Not quite."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["explain why files are needed","open files in READ, WRITE and APPEND modes","write lines to a file","read a file line by line until EOF","close files properly"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"Text file","meaning":"A file storing characters."},{"word":"Append","meaning":"Add to the end of a file."},{"word":"EOF","meaning":"End of file."},{"word":"CSV","meaning":"Comma-separated values."},{"word":"Persistent","meaning":"Kept after the program ends."}]},{"type":"sources","id":"sources","title":"Sources","items":["Python documentation: reading and writing files","Raspberry Pi Foundation, Isaac Computer Science: file handling","BBC Bitesize, GCSE Computer Science: file handling","RFC 4180: CSV format"]}]},"overlay":null}