Table of Contents
1. Reading from a File in Python in Hindi – फाइल से Read करना क्या है?
2. File Reading Methods in Hindi – read(), readline(), readlines()
4. Difference Between read(), readline(), readlines() in Hindi – तीनों में फर्क
Reading from a File in Python in Hindi – फाइल से Read करना क्या है?
Reading from a File का मतलब है किसी existing file (जैसे .txt file) के अंदर लिखे हुए data को Python program में लाकर उसे इस्तेमाल करना।
आसान भाषा में कहें तो, जैसे हम किसी notebook को खोलकर उसमें लिखी बातें पढ़ते हैं, वैसे ही Python किसी file को खोलकर उसका content पढ़ सकता है।
File से data read करने से पहले उस file को open() function की मदद से open करना जरूरी होता है, तभी उसका content access किया जा सकता है।
File को open करते समय mode भी बताना पड़ता है, जैसे "r" (read mode), जो बताता है कि file पर क्या operation करना है।
Python में file reading के लिए तीन main methods इस्तेमाल होते हैं – read(), readline() और readlines(), जो अलग-अलग तरीके से file का content return करते हैं।
File reading का इस्तेमाल तब सबसे ज्यादा होता है जब हमें किसी बड़े data (जैसे logs, reports, records) को program में process करना हो, बिना उसे manually type किए।
File को read करने के बाद उसे close() function से बंद करना भी जरूरी होता है, ताकि system resources properly free हो सकें।
Python में with statement का इस्तेमाल करके file को automatically close करने की सुविधा भी मिलती है, जिससे code ज्यादा safe और clean बनता है।
Example of File Reading – एक Simple शुरुआत
मान लो आपके पास "notes.txt" नाम की एक file है जिसमें कुछ text लिखा है। इसे read करने के लिए सबसे पहले file को open करना होगा।
file = open("notes.txt", "r")
content = file.read()
print(content)
file.close()
इस code में file को "r" यानी read mode में open किया गया, फिर read() method से उसका पूरा content एक साथ पढ़ लिया गया।
File Reading Methods in Hindi – read(), readline(), readlines()
1. read() Method
read() method पूरी file का content एक ही बार में एक single string के रूप में return करता है। इसे किसी particular number of characters के साथ भी इस्तेमाल किया जा सकता है।
file = open("notes.txt", "r")
data = file.read()
print(data)
file.close()
# सिर्फ पहले 10 characters पढ़ना
file = open("notes.txt", "r")
data = file.read(10)
print(data)
file.close()
2. readline() Method
readline() method file में से सिर्फ एक line पढ़ता है, यानी हर बार call करने पर वह file की अगली line return करता है।
file = open("notes.txt", "r")
line1 = file.readline()
line2 = file.readline()
print(line1)
print(line2)
file.close()
3. readlines() Method
readlines() method पूरी file की सभी lines को एक साथ पढ़कर उन्हें एक list के रूप में return करता है, जहाँ list का हर element file की एक line होती है।
file = open("notes.txt", "r")
all_lines = file.readlines()
print(all_lines)
file.close()
यह SVG file reading के तीनों methods के मुख्य काम को एक साथ दिखा रहा है।
Reading File Examples in Hindi – उदाहरण
Example 1: with Statement से File Read करना
with statement का इस्तेमाल करने से file को manually close() करने की जरूरत नहीं पड़ती, क्योंकि block खत्म होते ही file अपने आप बंद हो जाती है।
with open("notes.txt", "r") as file:
content = file.read()
print(content)
Example 2: Loop की मदद से Line-by-Line Read करना
अगर file को line by line process करना हो, तो सीधे file object पर for loop चलाया जा सकता है, जिससे हर बार एक line automatically मिलती जाती है।
with open("notes.txt", "r") as file:
for line in file:
print(line.strip())
Example 3: readlines() के साथ Loop इस्तेमाल करना
readlines() से मिली list को for loop की मदद से एक-एक करके print किया जा सकता है, जो एक बार में पूरी file के lines को memory में रखता है।
with open("notes.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(line.strip())
Example 4: File में कुल Lines Count करना
readlines() function का इस्तेमाल करके किसी file में मौजूद total lines की संख्या भी आसानी से निकाली जा सकती है।
with open("notes.txt", "r") as file:
lines = file.readlines()
print("Total lines:", len(lines))
Example 5: किसी Specific Line को पढ़ना
अगर सिर्फ किसी particular line number का data चाहिए, तो readlines() से मिली list में उस line का index इस्तेमाल किया जा सकता है।
with open("notes.txt", "r") as file:
lines = file.readlines()
print(lines[2])
Difference Between read(), readline(), readlines() in Hindi – तीनों में फर्क
| Point | read() | readline() | readlines() |
|---|---|---|---|
| Kaam (Function) | पूरी file का content एक साथ पढ़ता है | एक बार में सिर्फ एक line पढ़ता है | सभी lines को पढ़कर एक list बनाता है |
| Return Type | एक single string | एक single string (एक line के बराबर) | Strings की एक list, जहाँ हर element एक line है |
| Memory Usage | बड़ी files के लिए ज्यादा memory लग सकती है | सबसे कम memory इस्तेमाल करता है | readline() से ज्यादा, लेकिन controlled तरीके से |
| Use Case | जब पूरी file का content एक साथ चाहिए हो | जब file को line-by-line process करना हो | जब सभी lines को list के रूप में इस्तेमाल करना हो |
| Large Files पर Effect | बहुत बड़ी files के लिए efficient नहीं होता | बड़ी files के लिए सबसे efficient तरीका | बड़ी files में ज्यादा memory इस्तेमाल हो सकती है |
तीनों Methods को एक साथ Example से समझें
with open("notes.txt", "r") as file:
print(file.read())
with open("notes.txt", "r") as file:
print(file.readline())
with open("notes.txt", "r") as file:
print(file.readlines())
अगर "notes.txt" file में तीन lines "Hello", "Python" और "World" लिखी हों, तो read() पूरा text एक string में देगा, readline() सिर्फ "Hello" देगा, और readlines() ['Hello\n', 'Python\n', 'World\n'] जैसी एक list return करेगा।
यह SVG दिखा रहा है कि बड़ी files के लिए तीनों methods memory के हिसाब से किस तरह अलग-अलग असर डालते हैं।
Importance of File Reading in Hindi
1. Persistent Data को Access करना
File reading की मदद से program बंद होने के बाद भी save किया हुआ data दोबारा इस्तेमाल किया जा सकता है, जो memory (RAM) में possible नहीं होता।
2. Large Data को Process करना
Logs, reports या records जैसी बड़ी files को efficiently पढ़ने के लिए सही method (जैसे readline()) चुनना बहुत जरूरी होता है।
3. Automation में उपयोग
बहुत से automation scripts किसी file से data पढ़कर उसके आधार पर आगे की processing करते हैं, इसलिए file reading एक core skill है।
Advantages of File Reading in Hindi – फायदे
बड़ी मात्रा में data को बिना manually type किए program में लाया जा सकता है।
File reading के अलग-अलग methods जरूरत के हिसाब से memory को efficiently इस्तेमाल करने देते हैं।
with statement के साथ इस्तेमाल करने पर code ज्यादा safe और clean बनता है।
Automation, data analysis और log processing जैसे कामों में बहुत उपयोगी होता है।
Disadvantages of File Reading in Hindi – सीमाएं
अगर file exist नहीं करती, तो FileNotFoundError आ जाता है।
बहुत बड़ी files पर read() या readlines() इस्तेमाल करने से memory पर ज्यादा load पड़ सकता है।
File को manually close() न करने पर system resources properly free नहीं होते।
Beginners अक्सर read(), readline() और readlines() के return type को लेकर confuse हो जाते हैं।