Python me File Handling kya hai?


File Handling ka matlab hai computer ki files ke saath kaam karna — files ko padhna (read), likhna (write), kuch aur add karna (append) aur band karna (close). Real applications me data hamesha files me save hota hai — jaise notes app, login systems, log files etc.

Is page me yeh sab sikhenge :-

Note :- Yeh page ek interactive simulator hai. Real Python me files disk pe save hoti hain, lekin yahan hum ek virtual file system use karte hain taaki aap browser me hi sab kuch try kar sakein aur output dekh sakein.



1. File Open Karna — open() aur Modes

Python me file kholne ke liye open() function use hota hai. Isme file ka naam aur mode dena padta hai.

ModeMatlabFile nahi toh?
"r"Read — file padhna (default)FileNotFoundError
"w"Write — likhna (purana data mit jaata hai)Nai file ban jaati hai
"a"Append — end me add karnaNai file ban jaati hai
"x"Create — nai file bananaFileExistsError agar already hai
"r+"Read + Write donoFileNotFoundError

Example 1: open() modes ka demonstration

Python
In [1]:
1
Output


2. File Me Likhna — write() aur writelines()

"w" mode me file open karo aur write() se likhو. Agar file pehle se hai toh purana data delete ho jaata hai. writelines() se list ki saari lines ek saath likh sakte hain.

Example 1: File create karke likhna — write()

Python
In [1]:
1
Output

Example 2: writelines() — list ki saari lines ek saath likhna

Python
In [1]:
1
Output


3. File Padhna — read(), readline(), readlines()

File padhne ke 3 tarike hain — poori file ek saath, ek line, ya saari lines ki list.

MethodKaamReturn Type
read()Poori file ek string mestr
readline()Ek line padhostr
readlines()Saari lines ki listlist

Example 1: read() — poori file ek saath padhna

Python
In [1]:
1
Output

Example 2: readline() — ek ek line padhna

Python
In [1]:
1
Output

Example 3: readlines() — saari lines list me + loop se padhna

Python
In [1]:
1
Output


4. File Me Add Karna — Append Mode ("a")

Append mode me file ka purana data safe rehta hai aur naya data end me add hota hai. Yeh log files aur diaries ke liye perfect hai.

Example 1: Append — diary me roz nayi entry add karna

Python
In [1]:
1
Output

Example 2: Write vs Append — fark dekhna

Python
In [1]:
1
Output


5. with Statement — Best Practice

with statement se file automatically band ho jaati hai — chahe koi error aaye ya nahi. Yeh f.close() likhne se better aur safer hai. Professional code me hamesha with use karna chahiye.

Syntax
with open("filename.txt", "mode") as f:
    # yahan kaam karo
    # block khatam hone par file auto-close ho jaati hai

Example 1: with statement se file likhna aur padhna

Python
In [1]:
1
Output

Example 2: with + loop — multiple students ka data save karna

Python
In [1]:
1
Output


6. File Exist Check Karna — os Module

os module se file exist karti hai ya nahi check kar sakte hain, file rename, delete aur bahut kuch kar sakte hain.

Example 1: os.path.exists() — file exist karti hai ya nahi

Python
In [1]:
1
Output

Example 2: os.rename() aur os.remove() — file rename aur delete

Python
In [1]:
1
Output


7. CSV File Handling

CSV (Comma Separated Values) ek popular format hai data store karne ke liye — jaise Excel files. Python ka csv module isse bahut aasaan banata hai.

Example 1: CSV file likhna — csv.writer

Python
In [1]:
1
Output

Example 2: CSV file padhna — csv.reader aur DictReader

Python
In [1]:
1
Output



Quick Summary — File Handling ek nazar me

ConceptKaamExample
open("f","r")File read ke liye kholnaf = open("a.txt","r")
open("f","w")File write ke liye (reset)f = open("a.txt","w")
open("f","a")File append ke liyef = open("a.txt","a")
write()String likhnaf.write("hello\n")
writelines()List likhnaf.writelines(lst)
read()Poori file padhnaf.read()
readline()Ek line padhnaf.readline()
readlines()Saari lines list mef.readlines()
close()File band karnaf.close()
with open()Auto-close (best practice)with open(...) as f:
os.path.exists()File exist checkos.path.exists("f.txt")
os.rename()File rename karnaos.rename("a","b")
os.remove()File delete karnaos.remove("f.txt")
csv.writerCSV likhnacsv.writer(f).writerow()
csv.DictReaderCSV dict se padhnacsv.DictReader(f)