File Handling in Python - Class 12
File Handling in Python
Class 12 - Text File Handling
Presented in Simple Steps
What is File Handling?
It allows us to:
- Create files
- Write data
- Read data
- Append data
- Close files after use
File Modes in Python
"r" - Read
"w" - Write
"a" - Append
"x" - Create
Writing to a File
file = open("[Link]", "w")
[Link]("Hello")
[Link]()
Reading from a File
file = open("[Link]", "r")
content = [Link]()
print(content)
[Link]()
Appending to a File
file = open("[Link]", "a")
[Link]("\nNew Line")
[Link]()
Read Line by Line
file = open("[Link]", "r")
for line in file:
print(line)
[Link]()
Using with Statement
with open("[Link]", "r") as file:
data = [Link]()
print(data)
Tips for Teachers
- Start with theory
- Show live demo in IDLE
- Explain file modes
- Encourage hands-on practice