Practice: Appending Files
Q1. Append one line
Write code that appends the line `hello` to `a.txt` twice (two separate opens), then prints the file content.
Goal: Append a line to a file
Q2. Append numbers
Append the numbers 1 to 4, one per line, to `n.txt` using a loop, then print the lines joined by commas.
Goal: Write in a loop
Q3. Use print to file
Append the words `cat` and `dog` to `pets.txt` using print(..., file=f), then print how many lines the file has.
Goal: Use print with file=
Q4. writelines for a list
Given `items = ["x", "y", "z"]`, append each on its own line using writelines, then print the file.
Goal: Use writelines
Q5. Count lines after appending
Create `c.txt` with two lines using "w", append three more lines using "a", and print the total number of lines.
Goal: Combine w and a
Q6. Log function with levels
Write a function log(level, msg) that appends `LEVEL: msg` to `x.log`. Call it twice and print the file.
Goal: Build a log helper
Q7. Append only if missing
Write code that appends the name `Zed` to `names.txt` only if it is not already in the file. Run the logic twice and print the file.
Goal: Avoid duplicates when appending
Q8. Append in a loop efficiently
Open `t.txt` once in append mode and write squares of 1..4, one per line. Print the lines separated by spaces.
Goal: Efficient appending
Q9. Two appends
What does this print?
with open("o.txt", "a") as f:
f.write("A")
with open("o.txt", "a") as f:
f.write("B")
with open("o.txt") as f:
print(f.read())Goal: Predict append output
Q10. Write then append
What does this print?
with open("p.txt", "w") as f:
f.write("1\n")
with open("p.txt", "a") as f:
f.write("2\n")
with open("p.txt", "w") as f:
f.write("3\n")
with open("p.txt") as f:
print(f.read().split())Goal: Predict w versus a
Q11. writelines without newlines
What does this print?
with open("q.txt", "a") as f:
f.writelines(["a", "b", "c"])
with open("q.txt") as f:
print(f.read())Goal: Know writelines
Q12. print to file
What does this print?
with open("r.txt", "a") as f:
print("x", "y", file=f)
print("z", file=f)
with open("r.txt") as f:
print(f.readlines())Goal: Predict print to file
Q13. a+ without seek
What does this print?
with open("s.txt", "w") as f:
f.write("old")
with open("s.txt", "a+") as f:
data = f.read()
print(repr(data))Goal: Understand a+ position
Q14. seek then write
What does this print?
with open("u.txt", "w") as f:
f.write("hello")
with open("u.txt", "a+") as f:
f.seek(0)
f.write("X")
f.seek(0)
print(f.read())Goal: Append ignores seek for writes
Q15. Return value of write
What does this print?
with open("v.txt", "a") as f:
n = f.write("abc\n")
print(n)Goal: Know write's return value
Q16. Missing newline bug
This code appends two names but the file shows `AshaBen`. Fix it.
with open("d.txt", "a") as f:
f.write("Asha")
f.write("Ben")
with open("d.txt") as f:
print(f.read())Goal: Fix missing newlines
Q17. Lost history
A log keeps losing old entries. Find the bug and fix it.
def log(m):
with open("h.log", "w") as f:
f.write(m + "\n")
log("one")
log("two")Goal: Choose the right mode
Q18. writelines glued lines
The fixed program should print three separate lines.
with open("g.txt", "a") as f:
f.writelines(["red", "green", "blue"])
with open("g.txt") as f:
print(f.read())Goal: Fix writelines
Q19. Wrong mode crash
The code crashes with an error when it tries to write. Fix it so it prints `ok`.
with open("k.txt", "a") as f:
pass
with open("k.txt", "r") as f:
f.write("ok\n")Goal: Fix wrong mode
Q20. Why append?
Explain in two sentences when you would choose "a" over "w".
Goal: Compare modes