Practice: Appending Files

« Read the chapter

Q1. Append one line

BeginnerCoding

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

BeginnerCoding

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

BeginnerCoding

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

IntermediateCoding

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

IntermediateCoding

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

IntermediateCoding

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

AdvancedCoding

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

AdvancedCoding

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

BeginnerOutput

What does this print?

Code
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

BeginnerOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

AdvancedOutput

What does this print?

Code
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

AdvancedOutput

What does this print?

Code
with open("v.txt", "a") as f:
    n = f.write("abc\n")
print(n)

Goal: Know write's return value

Q16. Missing newline bug

BeginnerDebugging

This code appends two names but the file shows `AshaBen`. Fix it.

Code
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

BeginnerDebugging

A log keeps losing old entries. Find the bug and fix it.

Code
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

IntermediateDebugging

The fixed program should print three separate lines.

Code
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

IntermediateDebugging

The code crashes with an error when it tries to write. Fix it so it prints `ok`.

Code
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?

BeginnerConceptual

Explain in two sentences when you would choose "a" over "w".

Goal: Compare modes