Module 4
File I/O & Exception Handling in Python
20 mins read
Lesson 1: File Operations
1.1 Reading & Writing Files
Python uses the `open()` function with context managers (`with open(...) as f:`) to ensure safe file closure.
PYTHON
# Writing and Reading Files
with open("data.txt", "w") as f:
f.write("Hello, Master Coding!")
with open("data.txt", "r") as f:
content = f.read()
print(content)