Python में File Functions in hindi

Arpit Nageshwar
⏰ 4 min read

File Functions in Python in Hindi – पाइथन में File Functions क्या हैं?

Table of Contents

Introduction to File Functions – फाइल फंक्शन्स का परिचय

जब भी हम Python में किसी data को permanently store करना चाहते हैं, तो हमें variables पर depend नहीं रहना पड़ता, बल्कि उस data को एक file में save करना होता है। यही काम File Handling के through किया जाता है, और इसमें जो built-in functions इस्तेमाल होते हैं, उन्हें File Functions कहा जाता है।

  • File Functions की मदद से हम किसी भी file को open कर सकते हैं, उसमें data read कर सकते हैं, नया data write कर सकते हैं और अंत में उस file को properly close भी कर सकते हैं।

  • Python में program खत्म होते ही variables में स्टोर data खत्म हो जाता है, लेकिन अगर वही data file में save कर दिया जाए, तो वह हमेशा के लिए store रहता है और बाद में भी use किया जा सकता है।

  • File Functions का इस्तेमाल text files, log files, configuration files और यहां तक कि बड़े data processing programs में भी होता है, इसलिए competitive exams में भी इनसे questions पूछे जाते हैं।

  • इन functions को समझने से पहले यह जानना जरूरी है कि Python में किसी file पर काम करने का एक fixed sequence होता है — पहले file को open किया जाता है, फिर उस पर read या write operation किया जाता है, और अंत में उसे close कर दिया जाता है।

  • अगर हम इस sequence को सही तरीके से follow नहीं करते, तो data loss होने का risk रहता है या फिर file properly save नहीं हो पाती।

open() File को खोलना read() / write() Data पढ़ना या लिखना tell() / seek() close() File को बंद करना

यह diagram दिखाता है कि Python में किसी file पर काम करने का सही sequence क्या होता है — पहले open(), फिर read/write/tell/seek operations, और अंत में close()।

File Functions in Python in Hindi

अब हम एक-एक करके सभी important File Functions को detail में समझते हैं, साथ ही हर function का syntax और example भी देखेंगे ताकि exam में इन्हें आसानी से याद रखा जा सके।

1. open() Function

open() function किसी भी file को access करने का सबसे पहला step होता है। यह function file का नाम और mode लेकर एक file object return करता है, जिसके through आगे के सारे operations किए जाते हैं।

file = open("data.txt", "r")
  • पहला parameter file का नाम या path होता है।

  • दूसरा parameter mode होता है, जैसे "r" (read), "w" (write), "a" (append) और "r+" (read और write दोनों)।

  • अगर file exist नहीं करती और mode "w" या "a" है, तो Python खुद-ब-खुद नई file बना देता है।

  • अगर "r" mode में file exist नहीं करती, तो FileNotFoundError आती है, इसलिए exam में यह point पूछा जाता है।

2. close() Function

File पर काम पूरा होने के बाद उसे close करना बहुत जरूरी होता है। close() function file को properly बंद कर देता है, जिससे data सही तरीके से save हो जाता है और system resources भी free हो जाते हैं।

file = open("data.txt", "r")
# file पर operations
file.close()
  • अगर file को close नहीं किया जाए, तो कभी-कभी data properly save नहीं होता, खासकर write mode में।

  • बड़े programs में एक साथ कई files open होती हैं, इसलिए हर file को close करना memory के लिए भी जरूरी है।

  • आजकल with statement भी use किया जाता है, जिससे file अपने-आप close हो जाती है, लेकिन exam की दृष्टि से close() function का syntax याद रखना जरूरी है।

3. read() Function

read() function पूरी file का content एक साथ single string के रूप में return करता है। यह तब useful होता है जब file छोटी हो और पूरा content एक बार में चाहिए हो।

file = open("data.txt", "r")
content = file.read()
print(content)
file.close()
  • अगर हम read() में कोई number pass करें, जैसे read(10), तो यह सिर्फ पहले 10 characters ही return करता है।

  • बड़ी files के लिए पूरा content read() से load करना memory के लिए heavy पड़ सकता है, इसलिए तब readline() या readlines() ज्यादा बेहतर option होते हैं।

4. readline() Function

readline() function file में से एक बार में सिर्फ एक line पढ़ता है। अगर इसे बार-बार call किया जाए, तो यह अगली-अगली lines return करता जाता है।

file = open("data.txt", "r")
line1 = file.readline()
line2 = file.readline()
print(line1)
print(line2)
file.close()
  • यह उन cases में useful होता है जहां हमें file को line-by-line process करना होता है, जैसे log files पढ़ते समय।

  • अगर file में और lines नहीं बची हैं, तो readline() एक empty string return करता है।

5. readlines() Function

readlines() function पूरी file की सभी lines को एक साथ एक list के रूप में return करता है, जहां हर line list का एक अलग element होती है।

file = open("data.txt", "r")
lines = file.readlines()
print(lines)
file.close()
  • यह function तब काम आता है जब हमें सभी lines पर loop चलाकर कोई processing करनी हो।

  • हर element के अंत में newline character (\n) भी present रहता है, जिसे जरूरत पड़ने पर strip() करके हटाया जा सकता है।

6. write() Function

write() function किसी string को file में लिखने के लिए इस्तेमाल होता है। इसके लिए file को "w" या "a" mode में open करना जरूरी होता है।

file = open("data.txt", "w")
file.write("Hello Students!")
file.close()
  • "w" mode में write() पुराने content को पूरी तरह overwrite कर देता है।

  • अगर पुराना data रखते हुए नया data जोड़ना है, तो "a" (append) mode use किया जाता है।

  • write() function automatically newline नहीं जोड़ता, इसलिए अलग-अलग lines के लिए manually \n लगाना पड़ता है।

7. writelines() Function

writelines() function एक साथ multiple strings (एक list के रूप में) file में लिखने के लिए इस्तेमाल होता है।

file = open("data.txt", "w")
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
file.writelines(lines)
file.close()
  • write() की तरह ही writelines() भी automatically newline character नहीं जोड़ता, इसलिए list में हर string के अंत में \n manually देना पड़ता है।

  • यह function तब बहुत useful होता है जब हमारे पास पहले से ही data एक list के रूप में मौजूद हो।

8. tell() Function

tell() function यह बताता है कि file pointer अभी file में किस position पर मौजूद है। यह position bytes में return होती है।

file = open("data.txt", "r")
file.read(5)
position = file.tell()
print(position)
file.close()
  • File open करते ही pointer position 0 पर होता है।

  • जैसे-जैसे हम read() या write() करते जाते हैं, pointer आगे बढ़ता जाता है, और tell() हमें उसकी current location बता देता है।

9. seek() Function

seek() function file pointer को किसी specific position पर move करने के लिए इस्तेमाल होता है। इससे हम file के किसी भी particular जगह से data read या write कर सकते हैं।

file = open("data.txt", "r")
file.seek(0)
content = file.read()
print(content)
file.close()
  • seek(0) pointer को फिर से file की शुरुआत में ले आता है, ताकि content दोबारा से read किया जा सके।

  • seek() function tell() के साथ मिलकर बहुत useful होता है, खासकर जब हमें file के किसी specific हिस्से पर काम करना हो।

H e l l o ... tell() = 3 seek(0) Pointer वापस शुरुआत में

यह diagram दिखाता है कि file पढ़ते समय tell() function pointer की current position बताता है, और seek() function उसे वापस किसी भी position पर ले जा सकता है।

Summary Table of All File Functions in Python in Hindi

नीचे दी गई table में सभी important File Functions को एक जगह summarize किया गया है, ताकि exam से पहले quick revision की जा सके।

Function काम (Use) Return Value
open() File को open करना File object
close() File को बंद करना None
read() पूरी file या specified characters पढ़ना String
readline() एक बार में एक line पढ़ना String
readlines() सभी lines को list के रूप में पढ़ना List of strings
write() File में single string लिखना Characters की संख्या
writelines() File में multiple strings (list) लिखना None
tell() File pointer की current position बताना Integer (bytes)
seek() File pointer को किसी specific position पर ले जाना Integer (नई position)

इस table को अच्छी तरह याद रखने से competitive exams और university exams दोनों में File Handling से जुड़े questions आसानी से solve किए जा सकते हैं।

FAQs

Python में File Functions वे built-in functions होते हैं जिनकी मदद से हम किसी file को open, read, write और close कर सकते हैं। इनमें open(), close(), read(), write() जैसे functions शामिल होते हैं जो file handling के लिए इस्तेमाल होते हैं।
read() पूरी file का content एक single string के रूप में return करता है, जबकि readlines() पूरी file की सभी lines को एक list के रूप में return करता है, जहां हर line अलग element होती है।
tell() function यह बताता है कि file pointer अभी किस position पर है, जबकि seek() function pointer को किसी specific position पर move करने के लिए इस्तेमाल होता है। दोनों functions मिलकर file के अंदर navigation आसान बनाते हैं।
write() function एक single string को file में लिखता है, जबकि writelines() function multiple strings को एक list के रूप में एक साथ file में लिख देता है। दोनों ही functions automatically newline character नहीं जोड़ते।
File को close करना इसलिए जरूरी है ताकि लिखा गया data properly save हो जाए और system resources भी free हो जाएं। अगर file close न की जाए, तो कभी-कभी data loss होने का risk रहता है।
File Functions competitive exams और university exams दोनों में frequently पूछे जाते हैं क्योंकि File Handling programming का एक core concept है। इनकी syntax और उपयोग समझने से data storage और processing से जुड़े questions आसानी से solve हो जाते हैं।
Arpit Nageshwar

✍️ Arpit Nageshwar

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