Great 👍
🚀
Ab hum real-world Python me aa
gaye hain
Ye part projects, exams, jobs
sabke liye compulsory hota hai.
---
🐍 PYTHON PROGRAMMING
📘 PART 7: FILE HANDLING,
EXCEPTION HANDLING &
MODULES (FULL DETAIL)
> Is part ke baad tum:
File me data save kar paoge
Errors ko safely handle kar paoge
Python ke built-in tools use kar
paoge
---
🔷 SECTION 1: FILE HANDLING
1️⃣ File Handling kya hota hai?
📌 Simple words me:
Program ke data ko file me store
karna aur wapas read karna = File
Handling
🧠 Real-life example:
Marks ko notebook me likhna
Phone contacts save karna
---
2️⃣ File ke modes (Very Important)
Mode Use
r Read
w Write (purana data delete)
a Append (new data add)
r+Read + Write
---
3️⃣ File open & close
✍️ Syntax:
f = open("[Link]", "w")
[Link]()
---
4️⃣ File me write karna
f = open("[Link]", "w")
[Link]("Hello Python")
[Link]()
👉 Agar file nahi hai → Python
khud bana dega
---
5️⃣ File se read karna
f = open("[Link]", "r")
data = [Link]()
print(data)
[Link]()
---
6️⃣ Append mode (Important)
f = open("[Link]", "a")
[Link]("\nNew Line Added")
[Link]()
👉 Purana data safe rahega
---
7️⃣ with statement (Best Practice)
with open("[Link]", "r") as f:
print([Link]())
👉 close() likhne ki zarurat nahi
---
🔷 SECTION 2: EXCEPTION
HANDLING
8️⃣ Error kya hota hai?
Program chalate waqt agar
problem aaye → Error
Example:
Wrong input
File not found
Divide by zero
---
9️⃣ Exception Handling kya hota
hai?
📌 Simple language:
Error aaye to program crash na ho
---
🔹 try – except
try:
x = int(input("Enter number: "))
print(10 / x)
except:
print("Error occurred")
---
🔹 Specific Exception
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
---
🔹 finally block
try:
f = open("[Link]", "r")
print([Link]())
except:
print("File error")
finally:
print("Program ended")
👉 finally hamesha chalega
---
🔷 SECTION 3: MODULES &
PACKAGES
🔟 Module kya hota hai?
📌 Module:
Python file jisme functions /
variables ho
---
1️⃣1️⃣ Built-in Modules
🔹 math module
import math
print([Link](16))
print([Link](5))
---
🔹 random module
import random
print([Link](1, 10))
---
🔹 datetime module
import datetime
print([Link]())
---
1️⃣2️⃣ Custom Module banana
🔹 Step 1: file banao →
[Link]
def add(a, b):
return a + b
🔹 Step 2: use karo
import mymath
print([Link](5, 3))
---
1️⃣3️⃣ import ke tareeke
import math
from math import sqrt
import math as m
---
🧪 Practice Questions (Very
Important)
1️⃣ File me student names save karo
2️⃣ File se data read karo
3️⃣ Divide by zero error handle karo
4️⃣ math module se square root
nikaalo
5️⃣ Custom module banao
---
🎯 PART 7 RESULT
Ab tum:
Data permanently save kar sakte
ho
Errors ko smartly handle kar sakte
ho
Python modules use kar sakte ho
🔥 Ab tum strong intermediate
Python programmer ho
---
👉 Next & FINAL PART:
📘 PART 8:
Advanced Python + Libraries +
Projects + Career Roadmap
Bas likho: START PART 8 🚀