Reading and writing files
Read a text file line by line into values, and write results to a new file in the mode that keeps the data.
It opens on
A file is opened for writing and gets f.write("Ana,82") and then f.write("Bo,75"). Does the file then hold two lines or one?
Why it matters
A program that cannot keep or load data forgets everything when it ends; every grade book, log and data set is a file, and the beginner's file bugs are small and silent, a missing newline, a forgotten close, a file emptied by the wrong mode.
Good to know first
- String methods and immutability: strip removes the newline and split(",") cuts a line at the commas
- Input, output and type conversion: int turns the string "82" into the number 82
- for loops visit each item of a sequence in turn
Step by step
Write a file and read it back
Write text to a file with with open and write, include the newline each line needs, and read the whole file back as one string.
Reading the file back, the run printed Ana,82Bo,75. The two writes ran together on a single line, because write puts in exactly the text we give it and, unlike print, adds no newline. That answers the opening question.
what the file held
A common mix-up
Write adds a newline like print does, and closing the file is optional.
In fact: Write adds nothing, and without a close the text may not be in the file yet.
Read line by line
Loop over a file's lines, strip the newline, split at the comma, convert the score and total the scores.
The run printed 'Ana,82\n' and 'Bo,75\n'. Each line still ends with its newline, and the quotes and the backslash n of the repr show it. The newline is part of the line, so strip is needed.
A common mix-up
The line is just the text, so Ana,82 and "Ana,82" are the same.
In fact: The line keeps its newline until strip removes it, and the score is a string until int converts it.
Write a report and the traps
Read one file and write another with with blocks, choose the mode "w" or "a" correctly, and say what happens when a file is missing.
For each line, strip and split give the name and the score, int(s) >= 80 picks the word, and out.write puts the name, the word and a newline into the report. The report came out as three lines, Ana pass, Bo fail and Cy pass.
A common mix-up
Opening a file with "w" to add a line keeps the old lines.
In fact: "w" truncates the file at once, and only "a" keeps what was there.
Try it
What are the lines of a file after write("x"), write("y\n") and write("z")?Answer
Xy, then z
Hint: Only the second write ends a line
A file nums.txt has the lines 4, 9 and 2, one number to a line. Write a loop that prints their sum.Answer
Total = 0; for line in f: total = total + int(line.strip()); prints 15
Hint: Strip and int on each line
A log file log.txt holds old entries, and a student opens it with "w" to add a line at the end. What goes wrong, and what is the fix?Answer
The old entries are wiped, since "w" truncates; open it with "a"
Hint: Which mode keeps what is there
The file now has three lines, each ending in a newline. How many characters does it hold in all?Answer
19, which is 7 for Ana,82 and its newline, then 6 for Bo,75 and 6 for Cy,91, each newline counting as one character.
Why does total + score fail but total + int(score) work?Answer
Score is a string, and a number and a string cannot be added, where int(score) is the number 82.
