← Back

Home › Hindi › file handling Hindi में

Python file handling Hindi में: file read, write और append कैसे करें

🌐 यह page Hindi में है · Python tutorials in English →

r / w / a modeswith open()Examples + output

File handling क्या है और कहाँ चलाएँ?

File handling से program अपने चलने के बाद भी data को file में रख सकता है: text पढ़ना, नया data लिखना या existing data के अंत में जोड़ना। नीचे के examples अपने computer पर Python से चलाओ; PyDebug के browser playground में चलने वाला Python अलग in-browser filesystem में है — वहाँ बनी file आपके phone/computer के Downloads folder में अपने आप नहीं पहुँचती। हर example में notes.txt script की current working directory में माना गया है।

सबसे साफ तरीका with open(...) है: block खत्म होने पर file बंद हो जाती है, error आने पर भी।

write mode (w): नई file लिखना

w file न होने पर बनाता है और मौजूदा content मिटाकर शुरू से लिखता है। महत्वपूर्ण data पर गलती से w मत चलाओ। Text के लिए encoding बताना अच्छा है, खासकर Hindi लिखते समय।

with open("notes.txt", "w", encoding="utf-8") as f:
    f.write("नमस्ते Python\n")
    f.write("दूसरी line\n")
# notes.txt अब दो lines रखती है

write() खुद newline नहीं जोड़ता: अगली line चाहिए तो string में \n डालो। अगर file को overwrite होने से बचाना है तो x mode लगाओ, मौजूद file पर वह FileExistsError देगा।

read mode (r): content कैसे पढ़ें?

r पहले से मौजूद file पढ़ता है; नहीं मिली तो FileNotFoundError आता है। पिछला example चलाने के बाद यह code चलाओ:

with open("notes.txt", "r", encoding="utf-8") as f:
    text = f.read()
print(text, end="")
# output:
# नमस्ते Python
# दूसरी line

read() पूरा content एक string में लेता है। बड़ी file को line by line पढ़ने के लिए for line in f: इस्तेमाल करो; readline() हर call पर अगली एक line देता है। File कहाँ है यह unclear हो तो current directory और filename जाँचो, सिर्फ path का अंदाजा न लगाओ।

append mode (a): पुराना data बचाकर जोड़ना

a लिखने से पहले वाली lines रहती हैं और नया text अंत में जुड़ता है। File नहीं है तो बन जाती है।

with open("notes.txt", "a", encoding="utf-8") as f:
    f.write("तीसरी line\n")

with open("notes.txt", encoding="utf-8") as f:
    print(len(f.readlines()))
# output: 3 (अगर पहले वाले write example से शुरुआत की)

Default open mode r है, पर साफ code के लिए mode लिख देना बेहतर है। उसी file को बार-बार run करोगे तो append हर बार एक और line जोड़ देगा; तब output 3 से बढ़ जाएगा।

Missing file को safely handle करें

अगर file optional है तो सिर्फ expected exception पकड़ो। हर error को except: से चुपचाप छिपाना debugging मुश्किल करता है।

try:
    with open("missing.txt", encoding="utf-8") as f:
        print(f.read())
except FileNotFoundError:
    print("File नहीं मिली; नाम और path जाँचें")
# output: File नहीं मिली; नाम और path जाँचें

Exercises: एक file में तीन नाम लिखो, फिर for line in f से हर नाम पढ़ो। w और a को बदलकर देखो कि पिछली lines बचती हैं या नहीं। Python exceptions की detail Hindi guide में है।

अक्सर पूछे जाने वाले सवाल

Python में file read और write कैसे करते हैं?

with open('file.txt', 'r', encoding='utf-8') से पढ़ें; 'w' से लिखें। w पुराना content मिटाता है, इसलिए ध्यान से इस्तेमाल करें।

Python में w और a mode का फर्क क्या है?

w file को खाली करके लिखता है; a पुराना data रखता है और अंत में नया text जोड़ता है।

FileNotFoundError क्यों आता है?

r mode में दिया गया path नहीं मिलता, अक्सर गलत filename या current working directory की वजह से। नाम और path जाँचें।

Browser Python playground में लिखी file कहाँ जाती है?

वह browser के अलग runtime में रहती है; अपने phone की Downloads में अपने आप save नहीं होती। Local file practice के लिए computer पर Python चलाएँ।

आगे पढ़ो