Practice: Working with Text Files
Q1. Write two lines
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
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
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
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
Create `g.txt` with the lines `hi`, `hello`, `hey`. Print the longest line after stripping.
Goal: Process lines
Q6. Append a line
Write `first` to `ap.txt`, then append `second`. Print all lines joined with a comma.
Goal: Append to a file
Q7. Show hidden newline
What does this code print?
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
What does this code print?
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
What does this code print?
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
What does this code print?
with open("r.txt", "w") as f:
n = f.write("hello\n")
print(n)Goal: Know write returns a count
Q11. Empty second read
What does this code print?
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
What does this code print?
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
This code writes `a` and `b` on the same line. Fix it so they are on two lines, then print the file content.
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
This code prints a blank line after every line. Fix it so the lines print without gaps.
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
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.
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
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
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