Feedback Form

File Handling in python in hindi

File Handling in Python in Hindi – Complete Guide

File Handling in Python in Hindi

file handling का मतलब है — file को create करना, open करना, data read करना, data write करना और काम हो जाने के बाद file को safely close करना। Python इस पूरे process को बहुत easy और readable बना देता है।

File Handling kya hai in hindi

File एक ऐसा storage medium है जहाँ data permanently save रहता है, यानी program बंद होने के बाद भी data delete नहीं होता।

File handling mainly 5 basic steps में complete होती है:

  • File को open करना
  • File में data read या write करना
  • File mode decide करना
  • Error handling करना
  • File को close करना

File open karna Python me in hindi

Python में File Open करना

Python में file open करने के लिए open() function का उपयोग किया जाता है।

file = open("sample.txt", "r")
content = file.read()
print(content)
file.close()
---

समझें:

  • open("sample.txt", "r") → file को read mode में खोलता है
  • read() → file का पूरा content पढ़ता है
  • print() → content को output में दिखाता है
  • close() → file को बंद करता है
---

Output Example:

Hello World
This is a sample file

File modes in Python in hindi

Python में file खोलते समय हम mode बताते हैं, जिससे पता चलता है कि file को कैसे इस्तेमाल करना है (read, write, append आदि)।


1. Read Mode (r)

file = open("sample.txt", "r")
  • File को पढ़ने के लिए
  • File नहीं मिलने पर error आता है

2. Write Mode (w)

file = open("sample.txt", "w")
  • File में नया data लिखने के लिए
  • पुराना data delete हो जाता है
  • File नहीं है तो नई बन जाती है

3. Append Mode (a)

file = open("sample.txt", "a")
  • File के अंत में data जोड़ने के लिए
  • पुराना data सुरक्षित रहता है

4. Read + Write Mode (r+)

file = open("sample.txt", "r+")
  • Read और write दोनों कर सकते हैं
  • File पहले से होनी चाहिए

5. Write + Read Mode (w+)

file = open("sample.txt", "w+")
  • Read और write दोनों
  • पुराना data delete हो जाता है

6. Append + Read Mode (a+)

file = open("sample.txt", "a+")
  • Data जोड़ सकते हैं और पढ़ भी सकते हैं
  • File नहीं है तो बन जाएगी

7. Binary Modes (rb, wb, ab)

file = open("image.jpg", "rb")
  • Images, videos जैसे binary files के लिए

Summary

r  → पढ़ना
w  → लिखना (पुराना data हटेगा)
a  → जोड़ना
r+ → पढ़ना + लिखना
w+ → लिखना + पढ़ना (पुराना हटेगा)
a+ → जोड़ना + पढ़ना
rb → binary पढ़ना

File read karna Python me in hindi

Python में file को पढ़ने के लिए open() function और अलग-अलग methods का उपयोग किया जाता है।


1. पूरी file पढ़ना (read)

file = open("sample.txt", "r")
content = file.read()
print(content)
file.close()
  • read() पूरी file का content एक साथ पढ़ता है

2. एक line पढ़ना (readline)

file = open("sample.txt", "r")
line = file.readline()
print(line)
file.close()
  • readline() एक बार में एक line पढ़ता है

3. सभी lines पढ़ना (readlines)

file = open("sample.txt", "r")
lines = file.readlines()
print(lines)
file.close()
  • readlines() सभी lines को list के रूप में देता है

4. Loop के साथ पढ़ना (best तरीका)

file = open("sample.txt", "r")

for line in file:
    print(line)

file.close()
  • यह तरीका memory efficient होता है
  • बड़ी files के लिए best है

File write karna Python me in hindi

Python में file में data लिखने के लिए write() और writelines() method का उपयोग किया जाता है।


1. write() से file में लिखना

file = open("sample.txt", "w")
file.write("Hello World")
file.close()
  • write() text को file में लिखता है
  • w mode पुराना data delete कर देता है

2. कई lines लिखना

file = open("sample.txt", "w")
file.write("Hello\n")
file.write("Python\n")
file.close()
  • \n नई line के लिए use होता है

3. writelines() का उपयोग

file = open("sample.txt", "w")
lines = ["Hello\n", "World\n", "Python\n"]
file.writelines(lines)
file.close()
  • writelines() list के रूप में data लिखता है

4. Append mode में लिखना (a)

file = open("sample.txt", "a")
file.write("New Line Added\n")
file.close()
  • पुराना data delete नहीं होता
  • नई line अंत में जुड़ती है

5. Best तरीका (with statement)

with open("sample.txt", "w") as file:
    file.write("Hello Python")

File append karna Python me in hindi

Python में file में नया data जोड़ने (append करने) के लिए append mode (a) का उपयोग किया जाता है।


1. Append Mode (a) का उपयोग

file = open("sample.txt", "a")
file.write("New Line Added\n")
file.close()
  • a mode file के अंत में data जोड़ता है
  • पुराना data delete नहीं होता
  • File नहीं है तो नई file बन जाती है

2. Multiple Lines Append करना

file = open("sample.txt", "a")
file.write("Hello\n")
file.write("Python\n")
file.close()
  • हर line के लिए \n का उपयोग करें

3. writelines() के साथ append

file = open("sample.txt", "a")
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
file.writelines(lines)
file.close()
  • List के रूप में multiple lines जोड़ सकते हैं

4. Best तरीका (with statement)

with open("sample.txt", "a") as file:
    file.write("Appending using with\n")
  • File automatically close हो जाती है
  • यह सबसे safe और recommended तरीका है

Summary

a mode        → data जोड़ना
पुराना data   → सुरक्षित रहता है
write()       → एक line जोड़ना
writelines()  → multiple lines जोड़ना
with          → best तरीका

File close karna Python me in hindi

Python में file के साथ काम करने के बाद उसे बंद करना बहुत जरूरी होता है। इसके लिए close() method का उपयोग किया जाता है।


1. close() method का उपयोग

file = open("sample.txt", "r")
content = file.read()
print(content)
file.close()
  • close() file को बंद करता है
  • यह system resources को free करता है

2. क्यों जरूरी है file close करना?

  • Memory और resources बचाने के लिए
  • Data corruption से बचने के लिए
  • File को properly save करने के लिए

3. Best तरीका (with statement)

with open("sample.txt", "r") as file:
    content = file.read()
    print(content)
  • File automatically close हो जाती है
  • Manual close() की जरूरत नहीं होती

4. Check करना कि file बंद हुई या नहीं

file = open("sample.txt", "r")
print(file.closed)  # False

file.close()
print(file.closed)  # True
  • file.closed True/False में status बताता है

With statement in Python file handling in hindi

Python में with statement का उपयोग file handling को आसान और सुरक्षित बनाने के लिए किया जाता है।


1. with statement क्या है?

with statement एक ऐसा तरीका है जिसमें file automatically open और close हो जाती है। इससे हमें manually close() करने की जरूरत नहीं पड़ती।


2. Syntax

with open("sample.txt", "r") as file:
    content = file.read()
    print(content)
  • open() → file खोलता है
  • as file → file object को नाम देता है
  • block के खत्म होते ही file automatically close हो जाती है

3. Write करने के साथ with

with open("sample.txt", "w") as file:
    file.write("Hello Python")
  • File automatically close हो जाती है

4. Append करने के साथ with

with open("sample.txt", "a") as file:
    file.write("New Line Added\n")

Exception handling in file handling in hindi

Python में file handling करते समय कई बार error (exception) आ सकते हैं, जैसे file नहीं मिलना या permission issue। इन errors को handle करने के लिए try-except का उपयोग किया जाता है।


1. Basic try-except

try:
    file = open("sample.txt", "r")
    content = file.read()
    print(content)
    file.close()

except:
    print("Error आया है")
  • try block में risky code लिखा जाता है
  • except block error को handle करता है

2. Specific Exception Handling

try:
    file = open("sample.txt", "r")
    print(file.read())

except FileNotFoundError:
    print("File नहीं मिली")

except PermissionError:
    print("Permission नहीं है")
  • अलग-अलग errors को अलग handle कर सकते हैं

3. finally block

try:
    file = open("sample.txt", "r")
    data = file.read()

except:
    print("Error आया")

finally:
    print("यह हमेशा चलेगा")
  • finally हमेशा execute होता है (error हो या न हो)

4. with + try-except (Best Practice)

try:
    with open("sample.txt", "r") as file:
        data = file.read()
        print(data)

except FileNotFoundError:
    print("File नहीं मिली")
  • यह सबसे safe और recommended तरीका है

5. else block

try:
    file = open("sample.txt", "r")

except:
    print("Error आया")

else:
    print("File successfully खुल गई")
    file.close()
  • else तब चलता है जब कोई error नहीं आता

Summary

try        → risky code
except     → error handle
finally    → हमेशा चलेगा
else       → no error case
best तरीका → with + try-except

इस program में हम file create, write, read, append और exception handling सब कुछ एक साथ सीखेंगे।


Complete Python Program

try:
    # Write mode (नई file बनाना और लिखना)
    with open("sample.txt", "w") as file:
        file.write("Hello World\n")
        file.write("यह पहली लाइन है\n")

    # Append mode (नई line जोड़ना)
    with open("sample.txt", "a") as file:
        file.write("यह नई line add की गई है\n")

    # Read mode (file पढ़ना)
    with open("sample.txt", "r") as file:
        content = file.read()
        print("File Content:\n")
        print(content)

except FileNotFoundError:
    print("File नहीं मिली")

except Exception as e:
    print("Error:", e)

finally:
    print("\nProgram समाप्त हुआ")

Program की Working

  • पहले file बनाई और data लिखा (write mode)
  • फिर नई line add की (append mode)
  • फिर पूरी file पढ़ी (read mode)
  • Errors को handle किया (try-except)

FAQs

Python File Handling ka matlab hota hai Python program ke through file ke data ko read, write, append aur store karna। Iska use tab hota hai jab hume data ko permanently save karna ho। Simple words me, program band hone ke baad bhi data safe rahe, is process ko File Handling in hindi kehte hain।

Python me file open karne ke liye different modes use hote hain jaise r, w, a, r+। r mode file read karne ke liye, w mode data write karne ke liye aur a mode data append karne ke liye use hota hai। Ye modes File Handling in Python in hindi ka important exam topic hai।

File read ka matlab hota hai file se data lena, jabki file write ka matlab hota hai file me data save karna। read() method data ko program me laata hai aur write() method data ko file me store karta hai। Ye difference Python File Handling in hindi me basic aur most asked question hai।

File close karna isliye zaruri hota hai kyunki ye system resources free karta hai aur data ko safe banata hai। Agar file close nahi ki gayi, to data loss ya error aa sakta hai। Isliye File Handling in Python in hindi me file.close() ka use important mana jata hai।

with statement Python me file handling ko easy aur safe banata hai। Iska use karne par file automatically close ho jati hai, hume manually close nahi karni padti। Exam aur practical dono me with statement File Handling in hindi ka best practice mana jata hai।

Python practical exam me file handling se related programs aksar puche jate hain। Jaise file read karna, write karna, append karna aur exception handle karna। Agar student File Handling in Python in hindi ache se samajh leta hai, to practical exam me full marks lana easy ho jata hai।