Python for Beginners | Jan 31, 2026

Python for Beginners 21: Reading and Writing Files

A beginner-focused guide to Reading and Writing Files, including 10 practical Python examples and 20 quiz questions with answer guidance.

Beginner-Friendly Overview

Reading and Writing Files is a beginner-friendly milestone in Python. Think of it as learning one reliable tool before building a full toolbox.

In simple words, this lesson shows what the concept is, why it matters, and how to use it safely. The goal is clarity: you should be able to explain it to another beginner after reading.

Professional developers still rely on these basics every day. If you master this topic early, writing real-world code becomes much easier.

Detailed explanation of Reading and Writing Files:
1. Start with the smallest working example.
2. Name variables clearly so your future self understands the code.
3. Make one change at a time and rerun the program.
4. Read errors calmly: the message usually points to the exact issue.
5. Prefer simple code over clever code when you are learning.

Beginner mindset:
You are not expected to memorize everything. You are expected to practice, make mistakes, and improve. Write code daily, even if it is only a few lines.

Hands-On Examples

10 examples
Example 1
Read text file
main.py
1 with open('notes.txt', 'r', encoding='utf-8') as f:
2 print(f.read())

Run this code, change one value, and observe the output.

Example 2
Write text file
main.py
1 with open('output.txt', 'w', encoding='utf-8') as f:
2 f.write('Python practice')

Run this code, change one value, and observe the output.

Example 3
Append to file
main.py
1 with open('log.txt', 'a', encoding='utf-8') as f:
2 f.write('New entry\n')

Run this code, change one value, and observe the output.

Example 4
Try-except basics
main.py
1 try:
2 x = int('abc')
3 except ValueError:
4 print('Please enter a number')

Run this code, change one value, and observe the output.

Example 5
Try-except-else
main.py
1 try:
2 x = int('12')
3 except ValueError:
4 print('Bad input')
5 else:
6 print('Valid:', x)

Run this code, change one value, and observe the output.

Example 6
Finally block
main.py
1 f = None
2 try:
3 f = open('data.txt', 'r', encoding='utf-8')
4 finally:
5 if f:
6 f.close()

Run this code, change one value, and observe the output.

Example 7
Raise custom error
main.py
1 age = -1
2 if age < 0:
3 raise ValueError('Age cannot be negative')

Run this code, change one value, and observe the output.

Example 8
Handle missing keys
main.py
1 data = {'name': 'Kojo'}
2 try:
3 print(data['email'])
4 except KeyError:
5 print('Email missing')

Run this code, change one value, and observe the output.

Example 9
CSV reading idea
main.py
1 import csv
2 with open('users.csv', newline='', encoding='utf-8') as f:
3 rows = list(csv.DictReader(f))
4 print(len(rows))

Run this code, change one value, and observe the output.

Example 10
Log errors
main.py
1 import logging
2 logging.basicConfig(level=logging.INFO)
3 logging.info('Application started')

Run this code, change one value, and observe the output.

Quiz Section

20 questions
1. What problem does Reading and Writing Files solve in everyday coding?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
2. Which beginner mistake appears most often when learning Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
3. How do you explain Reading and Writing Files to someone who has never coded before?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
4. When should you avoid using Reading and Writing Files and choose a simpler approach?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
5. What is one clear sign that you understood Reading and Writing Files correctly?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
6. How does Reading and Writing Files make code easier to read?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
7. How does Reading and Writing Files help reduce bugs?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
8. What is a real-life analogy for Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
9. What happens if you skip learning Reading and Writing Files and jump ahead?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
10. How can you practice Reading and Writing Files in 15 minutes daily?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
11. Which Python built-in works nicely with Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
12. How do comments improve learning while practicing Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
13. What is the difference between theory and practical use in Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
14. How does Reading and Writing Files support teamwork in real projects?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
15. What interview question can test beginner knowledge of Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
16. How can you test your code while practicing Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
17. What naming style makes Reading and Writing Files examples easier to follow?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
18. How do you debug common errors while learning Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
19. What mini-project can strengthen your understanding of Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
20. What should be your next topic after learning Reading and Writing Files?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
Practice Homework

1. Re-type all examples without copy-paste.
2. Change values and predict the output before running.
3. Build one mini exercise that combines at least three examples.
4. Explain your solution in plain language.

WhatsApp