Concept of Files in Python in Hindi – Python में Files क्या हैं?

Arpit Nageshwar
⏰ 6 min read

Concept of Files in Python in Hindi – Python में Files क्या हैं?

Table of Contents

Concept of Files in Hindi – फाइल्स क्या हैं?

  • जब भी हम कोई Python program run करते हैं, तो उसमें बनाई गई सारी variables और data सिर्फ तब तक ही मौजूद रहते हैं जब तक program चल रहा होता है — program खत्म होते ही RAM से सब कुछ delete हो जाता है। अगर हमें data को permanently store करना हो, तो उसके लिए Files का इस्तेमाल किया जाता है।

  • File एक ऐसा named location होता है जो disk (जैसे hard disk या SSD) पर data को permanently store करने के लिए इस्तेमाल होता है, ताकि program बंद होने के बाद भी वो data मौजूद रहे।

  • आसान भाषा में कहें तो, अगर variables को हम एक temporary notepad मान लें, तो File एक permanent डायरी की तरह है, जिसमें लिखी गई चीज़ें डायरी बंद करने (यानी program खत्म होने) के बाद भी वहीं की वहीं रहती हैं।

  • Python में files को handle करने के लिए built-in functions मिलते हैं, जैसे open(), read(), write() और close(), जिनकी मदद से हम किसी भी file को create, read, update या delete कर सकते हैं।

  • रोज़-मर्रा की life से example लें तो, जब आप कोई Word document में notes लिखकर save करते हैं, तो वो notes उस file में permanently store हो जाते हैं — computer बंद करने के बाद भी वो notes वहीं रहते हैं। Python में file handling भी बिल्कुल इसी concept पर काम करती है।

  • Exam की भाषा में इसकी definition याद रखनी हो तो: "A File is a named collection of data stored on a secondary storage device, which allows data to persist even after the program that created it has terminated."

  • File handling का इस्तेमाल बहुत सारी जगहों पर होता है — जैसे logs store करना, user data save करना, configuration settings रखना, या बड़ी मात्रा में data को analyze करना।

Types of Files in Hindi – Text और Binary Files

Python में files मुख्य रूप से दो categories में बाँटी जाती हैं — Text Files और Binary Files। दोनों के अंदर data store करने का तरीका बिल्कुल अलग होता है।

Types of Files Text Files • Human-readable characters • Data ASCII/Unicode format mein store hota hai • Kisi bhi text editor mein khul jaati hai (Notepad) • Line-by-line data hota hai • Extension: .txt, .csv, .py • Mode: 'r', 'w', 'a' (default) Binary Files • 0 aur 1 (bytes) ke form mein data store hota hai • Direct text editor mein padha nahi jaa sakta • Special software/program chahiye padhne ke liye • Extension: .jpg, .mp4, .exe • Mode: 'rb', 'wb', 'ab'

यह diagram दिखा रहा है कि Text Files में data human-readable characters के रूप में store होता है और उसे किसी भी text editor में खोला जा सकता है, जबकि Binary Files में data 0 और 1 के form में store होता है, जिसे पढ़ने के लिए particular software की ज़रूरत पड़ती है।

1. Text Files

Text Files में data ASCII या Unicode characters के रूप में store होता है, जो human-readable होता है — यानी इसे किसी भी normal text editor (जैसे Notepad) में खोलकर सीधा पढ़ा जा सकता है। इसका data लाइन-दर-लाइन (line-by-line) organized होता है। Example: .txt, .csv, .py जैसी files।

# Text file likhna
with open("notes.txt", "w") as file:
    file.write("Yeh ek text file hai.\nPython file handling seekh rahe hain.")

# Text file padhna
with open("notes.txt", "r") as file:
    content = file.read()
    print(content)

2. Binary Files

Binary Files में data 0 और 1 (bytes) के रूप में store होता है, जो human-readable नहीं होता — इसे पढ़ने के लिए particular software या program चाहिए होता है। Example: images (.jpg), videos (.mp4), executable files (.exe)।

# Binary mode mein file open karna
with open("image.jpg", "rb") as file:
    data = file.read()
    print(type(data))    # Output: <class 'bytes'>

यह table format में भी देख सकते हैं:

Point Text File Binary File
Data Format ASCII/Unicode characters 0 aur 1 (bytes) ke roop mein
Readability Human-readable, direct padh sakte hain Human-readable nahi hoti
Editor Kisi bhi text editor mein khul jaati hai Special software chahiye
Example Extensions .txt, .csv, .py, .html .jpg, .mp4, .exe, .pdf (kuch)
Access Mode 'r', 'w', 'a' 'rb', 'wb', 'ab'

Characteristics of Files in Hindi – फाइल्स की विशेषताएं

1. Persistent Storage

File में स्टोर हुआ data program बंद होने के बाद भी disk पर बना रहता है, यानी यह data permanently store होता है, RAM की तरह temporary नहीं।

2. Unique Name और Location

हर File का एक नाम और एक path (location) होता है, जिससे उसे system में आसानी से ढूँढा और access किया जा सकता है।

3. Sequential या Random Access Possible

File के data को शुरू से आखिर तक क्रम में (sequential) पढ़ा जा सकता है, या फिर किसी particular position पर सीधे jump (random access) करके भी पढ़ा जा सकता है।

4. अलग-अलग तरह का Data Store कर सकती है

File में सिर्फ simple text ही नहीं, बल्कि images, videos, audio, या structured data (जैसे JSON, CSV) भी store किया जा सकता है।

5. Open और Close करना ज़रूरी

File को इस्तेमाल करने से पहले उसे open करना ज़रूरी होता है, और काम खत्म होने के बाद उसे close करना भी ज़रूरी होता है, वरना data properly save नहीं हो पाता या memory leak हो सकती है।

6. File Extension से Type पता चलता है

File का extension (जैसे .txt, .jpg) यह बताता है कि उसमें किस तरह का data store है, और उसे किस software से खोलना चाहिए।

Advantages of File Storage in Hindi – फाइल स्टोरेज के फायदे

  • Data program बंद होने के बाद भी permanently store रहता है, यानी data कभी खोता नहीं है।

  • बहुत बड़ी मात्रा में data (जैसे लाखों records) को आसानी से store और manage किया जा सकता है, जो RAM में संभव नहीं है।

  • एक बार बनी हुई File को अलग-अलग programs या systems के बीच आसानी से share किया जा सकता है।

  • Files की मदद से data को structured तरीके से organize किया जा सकता है, जिससे बाद में उसे ढूँढना और process करना आसान हो जाता है।

  • File-based storage तुलनात्मक रूप से बहुत सस्ता (cost-effective) होता है, database जैसी complex setup की ज़रूरत नहीं पड़ती।

  • Backup और recovery करना आसान होता है, क्योंकि files को कहीं और copy करके सुरक्षित रखा जा सकता है।

File Handling Process in Hindi – फाइल हैंडलिंग प्रोसेस

Python में किसी भी File को इस्तेमाल करने के लिए एक fixed process follow किया जाता है — Open करना, कोई operation (Read/Write) करना, और फिर उसे Close करना।

File Handling Process 1. File Open करना – open() 2. Mode Decide करना (r, w, a, rb...) 3. Operation करना (read/write/append) 4. File Close करना – close() Data safely disk par save ho jaata hai

यह diagram दिखा रहा है कि File Handling हमेशा एक fixed sequence में होती है — पहले File Open होती है, फिर उसका access mode decide होता है, फिर actual operation (जैसे पढ़ना या लिखना) होता है, और आखिर में File को Close कर दिया जाता है।

Code के through step-by-step देखें तो:

# Step 1 & 2: File open karna, mode ke saath
file = open("data.txt", "w")

# Step 3: Operation karna (yahan likhna)
file.write("Yeh Python file handling ka example hai.")

# Step 4: File close karna
file.close()

इसका एक और बेहतर तरीका with statement के साथ है, जो File को automatically close कर देता है, चाहे कोई error आए या ना आए:

with open("data.txt", "r") as file:
    content = file.read()
    print(content)
# yahan file already close ho chuki hogi, manually close karne ki zaroorat nahi

File Access Modes in Hindi – फाइल Modes

Mode Naam काम
'r'Read ModeFile ko sirf padhne ke liye kholta hai (default mode)
'w'Write ModeNaya data likhta hai, purana data delete kar deta hai
'a'Append ModePurane data ke aakhir mein naya data add karta hai
'x'Exclusive Create ModeNayi file banata hai, agar pehle se ho to error deta hai
'rb'Read BinaryBinary file ko padhne ke liye kholta hai
'wb'Write BinaryBinary file mein data likhne ke liye kholta hai

Points to Remember in Hindi – ध्यान रखने वाली बातें

  • File को हमेशा इस्तेमाल के बाद close ज़रूर करना चाहिए, वरना data properly save नहीं हो सकता या system resources block रह सकते हैं।

  • with statement का इस्तेमाल करना ज़्यादा safe माना जाता है, क्योंकि यह File को automatically close कर देता है।

  • 'w' mode में file open करने पर, अगर उस नाम की file पहले से मौजूद है, तो उसका पूरा पुराना data delete हो जाता है — इसलिए इसे सावधानी से इस्तेमाल करना चाहिए।

  • Text Files aur Binary Files को handle करने के modes अलग होते हैं ('r' vs 'rb'), इसलिए सही mode चुनना ज़रूरी है, वरना error आ सकता है।

  • File को handle करते समय try-except का इस्तेमाल करना अच्छी practice है, क्योंकि file ना मिलने पर (FileNotFoundError) program crash हो सकता है।

FAQs

File ek named location hoti hai jo disk par data ko permanently store karti hai, taaki program band hone ke baad bhi wo data mojood rahe। Python mein files ko open(), read(), write() aur close() jaise functions se handle kiya jaata hai।
Text File mein data human-readable characters ke roop mein store hota hai aur ise kisi bhi text editor mein padha ja sakta hai, jabki Binary File mein data 0 aur 1 (bytes) ke roop mein store hota hai, jise padhne ke liye particular software chahiye hota hai।
File Handling Process mein mukhya taur par File Open karna, uska access mode decide karna, koi operation (read/write/append) karna, aur aakhir mein File ko Close karna shamil hota hai।
File storage se data permanently save rehta hai, bade data ko efficiently manage kiya ja sakta hai, files ko share karna aasan hota hai, aur backup ya recovery bhi aasani se ki ja sakti hai।
with statement File ko automatically close kar deta hai, chahe code mein koi error kyun na aa jaaye। Isse manually close() call karna bhoolne ka risk khatam ho jaata hai aur code safe ban jaata hai।
'w' (write) mode file ka pura purana data delete karke naya data likhta hai, jabki 'a' (append) mode purane data ko waise hi rehne deta hai aur naya data sirf uske aakhir mein add karta hai।
Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified