Practice: Working with Text Files

« Read the chapter

Q1. Write two lines

BeginnerCoding

Write the lines `one` and `two` to `t.txt` and print the content of the file.

Goal: Write and read text

Q2. Number the lines

BeginnerCoding

Create `l.txt` with the lines `a`, `b`, `c`. Read it and print each line with its number starting at 1, like `1: a`.

Goal: Loop over lines

Q3. Use writelines

IntermediateCoding

Use writelines to write `x`, `y`, `z` (one per line) to `w.txt`, then print the lines as a list using readlines and strip.

Goal: Use writelines

Q4. Count words

BeginnerCoding

Write `the cat sat on the mat` to `c.txt`. Read it and print how many words it has.

Goal: Count words

Q5. Find the longest line

IntermediateCoding

Create `g.txt` with the lines `hi`, `hello`, `hey`. Print the longest line after stripping.

Goal: Process lines

Q6. Append a line

IntermediateCoding

Write `first` to `ap.txt`, then append `second`. Print all lines joined with a comma.

Goal: Append to a file

Q7. Show hidden newline

BeginnerOutput

What does this code print?

Code
with open("r.txt", "w") as f:
    f.write("ab\ncd\n")
with open("r.txt") as f:
    print(repr(f.readline()))

Goal: Know that lines keep newline

Q8. readlines result

BeginnerOutput

What does this code print?

Code
with open("r.txt", "w") as f:
    f.write("1\n2\n3\n")
with open("r.txt") as f:
    print(f.readlines())

Goal: Use readlines

Q9. Read in pieces

IntermediateOutput

What does this code print?

Code
with open("r.txt", "w") as f:
    f.write("abcdef")
with open("r.txt") as f:
    print(f.read(2), f.read(3), f.read())

Goal: Understand file position

Q10. write return value

IntermediateOutput

What does this code print?

Code
with open("r.txt", "w") as f:
    n = f.write("hello\n")
print(n)

Goal: Know write returns a count

Q11. Empty second read

IntermediateOutput

What does this code print?

Code
with open("r.txt", "w") as f:
    f.write("data")
with open("r.txt") as f:
    a = f.read()
    b = f.read()
print(len(a), len(b))

Goal: Understand the file position

Q12. Words per line

AdvancedOutput

What does this code print?

Code
with open("r.txt", "w") as f:
    f.write("a b\n\nc d e\n")
with open("r.txt") as f:
    print([len(line.split()) for line in f])

Goal: Handle blank lines

Q13. Lines run together

BeginnerDebugging

This code writes `a` and `b` on the same line. Fix it so they are on two lines, then print the file content.

Code
with open("d.txt", "w") as f:
    f.write("a")
    f.write("b")
with open("d.txt") as f:
    print(f.read())

Goal: Add newlines when writing

Q14. Double spacing

BeginnerDebugging

This code prints a blank line after every line. Fix it so the lines print without gaps.

Code
with open("e.txt", "w") as f:
    f.write("x\ny\n")
with open("e.txt") as f:
    for line in f:
        print(line)

Goal: Remove the newline

Q15. Erased content

IntermediateDebugging

The programmer wants the file to hold both `a` and `b`, but it only holds `b`. Fix it so that both remain and print them joined by a space.

Code
with open("f.txt", "w") as f:
    f.write("a\n")
with open("f.txt", "w") as f:
    f.write("b\n")
with open("f.txt") as f:
    print(f.read().split())

Goal: Choose append mode

Q16. Loop or readlines

IntermediateConceptual

Why is looping over the file object usually better than readlines() for a very large file?

Goal: Choose a reading method

Q17. Simple log filter

IntermediateApplication

Create `app.log` with the lines `INFO start`, `ERROR disk`, `INFO run`, `ERROR net`. Read it and print only the lines that start with ERROR.

Goal: Filter lines from a file