Home/Seekho/Files aur Data/File modes aur context managers
25 minIntermediateLesson 1/5

File modes aur context managers

Correct mode se resources open aur reliably close karo.

PDF · Chapter 25 · p.49-50
1 · Samjho

Idea, words aur real-world use pehle padho.

2 · Karke dekho

Example run karo, phir code task solve karo.

3 · Samjhaao

Quick check aur apne notes se concept explain karo.

Is lesson ke baad
  • File Modes and Context Managers ko apne words mein explain kar paana.
  • Working example ko run aur modify kar paana.
Yahin se start karo - pehle se kuch aana zaroori nahi

Kisi question ka answer dene se pehle hum samjhenge ki File Modes and Context Managers ka matlab kya hai, ye kahan useful hai aur example kaise behave karta hai. Naye words aate hi explain kiye gaye hain.

Idea ko samjho

File modes intended access describe karte hain. with block exception ke baad bhi file close karta hai.

Real-world connection

Library checkout record session end par book return ensure karta hai.

Example ko step-by-step padho

  1. 1Program mein banaye gaye names aur values pehchaano.
  2. 2Har line ko top-to-bottom trace karo aur dekho wo kya change karti hai.
  3. 3Run Python dabane se pehle output predict karo, phir ek value change karo.

Kya yaad rakhna hai

  • Correct mode se resources open aur reliably close karo.
  • Mistake useful feedback hai: error ki last line padho, ek cheez change karo aur dobara run karo.
Interactive concept lab

Concept ko step-by-step dekho

Arrows use karke data ka safar samjho.

3D flow · 1/3
Safely kholo
with open(path) as file
01

Safely kholo

Context manager file ko khud close karta hai.

Runnable example

File Modes and Context Managers: working example

Example run karo, phir ek value change karke apni understanding test karo.

example.py
Python 3.14 · 5s limit
Quick check · 10 points

Kaunsa mode existing content delete kiye bina append karta hai?

Intermediate
Coding task · 20 points

Code ko complete karo

In-memory file ki first line read karo.

challenge.py
Python 3.14 · 5s limit

Key takeaways

  • File modes intended access describe karte hain. with block exception ke baad bhi file close karta hai.
  • Is idea ko seekhne ka fastest way example ko change karke rerun karna hai.
Previous

Pass the quick check and coding task to complete this lesson.