0% found this document useful (0 votes)
12 views19 pages

Reading and Writing in A Text File

This lesson plan introduces Year 11 students to file input and output (I/O) in Python, covering how to open, read from, write to, and close files, along with different modes of file operations and best practices. The lesson is structured into several parts, including practical exercises and homework assignments to reinforce learning. It emphasizes the importance of file handling and error management in Python programming.

Uploaded by

teboho.moqasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Reading and Writing in A Text File

This lesson plan introduces Year 11 students to file input and output (I/O) in Python, covering how to open, read from, write to, and close files, along with different modes of file operations and best practices. The lesson is structured into several parts, including practical exercises and homework assignments to reinforce learning. It emphasizes the importance of file handling and error management in Python programming.

Uploaded by

teboho.moqasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lesson Plan: Reading and

Writing to Files in Python

Lesson Overview

This lesson will introduce Year 11


students to the concept of file
input and output (I/O) in Python.
Students will learn how to open,
read from, write to, and close
files. The lesson will cover
different modes of file operations
and best practices for handling
files.

Lesson Duration

80 minutes
Lesson Objectives

By the end of this lesson,


students will be
able to:

Understand the importance of file


I/O in Python.
Open, read from, write to, and
close files using Python.
Use different modes for file
operations.
Implement best practices for file
handling.

Materials Needed

Computers with Python installed


Text editor (e.g., IDLE, VSCode)
Sample text files for reading and
writing

Lesson Structure
Introduction (10 minutes)

Welcome and Overview


Briefly introduce the topic of file
I/O.
Explain the importance of file I/O
in Python for data storage and
retrieval.
Objectives
Outline the goals of the lesson.

Part 1: Opening and Closing


Files (15 minutes)

Opening Files
Explain the open() function and
its syntax: file = open(filename,
mode) .
Discuss different modes: 'r' ,
'w' , 'a' , 'x' , 'b' , 't' .
Demonstrate opening a file in
read mode:

file = open('example.txt', 'r')


Closing Files
Emphasize the importance of
closing files to prevent resource
leaks.
Show how to close a file:
file.close()

Introduce the with statement for


safer file handling:

with open('example.txt', 'r') as


file:
contents = file.read()

Part 2: Reading from Files (20


minutes)

Reading Entire File


Use the read() method to read
the entire content of a file:

with open('example.txt', 'r') as


file:
contents = file.read()
print(contents)

Reading Line by Line


Use the readline() method to
read a single line:

with open('example.txt', 'r') as


file:
line = file.readline()
print(line)

Use the readlines() method to


read all lines into a list:

with open('example.txt', 'r') as


file:
lines = file.readlines()
print(lines)

Part 3: Writing to Files (20


minutes)
Writing to a File
Open a file in write mode:

with open('output.txt', 'w') as file:


file.write('Hello, World!')

Appending to a File
Open a file in append mode:

with open('output.txt', 'a') as file:


file.write('\nAppended text')

Part 4: Copying Content


Between Files (10 minutes)

Copying Content
Demonstrate copying content
from one file to another:

with open('input.txt', 'r') as


input_file:
with open('output.txt', 'w') as
output_file:
for line in input_file:
output_file.write(line)
Part 5: Best Practices and Error
Handling (5 minutes)

Best Practices
Always close files or use the
with statement.
Handle exceptions to manage
errors gracefully.
Error Handling
Use try-except blocks to handle
file operations:

try:
with open('example.txt', 'r') as
file:
contents = file.read()
print(contents)
except FileNotFoundError:
print('File not found')
Conclusion and Q&A (5 minutes)

Summary
Recap the key points covered in
the lesson.
Questions
Open the floor for any questions
from the students.

Practical Exercise (15 minutes)

Task
Create a Python script that reads
content from a file, modifies it,
and writes the modified content
to a new file.
Instructions
Provide a sample text file.
Ask students to read the content,
make a specific change (e.g.,
replace a word), and write the
modified content to a new file.

Homework
Assign a task to create a Python
script that reads data from a CSV
file and writes a summary of the
data to a new text file.

References

https://tutorpython.com/file-input-
output-in-python/
https://hexlet.io/courses/python-
io/lessons/opening-and-closing/
theory_unit
https://www.geeksforgeeks.org/
read-content-from-one-file-and-
write-it-into-another-file/
https://slideplayer.com/slide/
14669441/
https://
programminghistorian.org/en/
lessons/working-with-text-files
https://
nsworldinfo.medium.com/
harnessing-the-power-of-input-
output-and-file-handling-in-
python-8a1fa72900b3
https://wiingy.com/learn/python/
file-
handling-in-python/

You might also like