Writing onto a File in Python in Hindi – File में Data कैसे लिखें?
Table of Contents
- 1. File Writing Methods (write(), writelines())
- 2. Writing File Examples in Python in Hindi
- 3. Appending Data to Files in Hindi
- 4. Difference Between write() and writelines() in Python in Hindi
1. File Writing Methods (write(), writelines())
Python में किसी file में data लिखने के लिए सबसे पहले उस file को open() function की मदद से open करना पड़ता है, और साथ में यह भी बताना होता है कि हम file को किस mode में open कर रहे हैं। data लिखने के लिए हम "w" (write mode) या "a" (append mode) का use करते हैं।
File में data लिखने के लिए Python दो मुख्य methods देता है:
1. write() Method:
यह method एक single string को file में लिख देता है।2. writelines() Method:
यह method एक list of strings को एक साथ file में लिख देता है।
file = open("notes.txt", "w")
file.write("Python सीखना बहुत आसान है।")
file.close()
ऊपर दिए गए example में हमने "w" mode में file open की, फिर write() method से एक line file में लिख दी, और आखिर में file.close() की मदद से file को close कर दिया। file को close करना बहुत जरूरी होता है, वरना data ठीक तरीके से save नहीं हो पाता।
यह SVG दिखा रहा है कि Python में किसी file में data लिखने के लिए open, write और close — यह तीन steps किस क्रम में follow किए जाते हैं।
2. Writing File Examples in Python in Hindi
आइए अब कुछ practical examples देखते हैं जिनसे write() और writelines() दोनों methods को अच्छे से समझा जा सके।
Example 1: write() से single line लिखना
file = open("students.txt", "w")
file.write("Rohit ने Python course join किया।")
file.close()
Example 2: write() से multiple lines लिखना
अगर हमें write() से एक से ज्यादा lines लिखनी हैं, तो हमें हर line के आखिर में \n (new line character) लगाना होता है, वरना सभी lines आपस में जुड़ जाएँगी।
file = open("students.txt", "w")
file.write("Rohit ने Python course join किया।\n")
file.write("Anjali ने भी course join किया।\n")
file.close()
Example 3: writelines() से एक साथ कई lines लिखना
lines = ["Rohit ने Python course join किया।\n",
"Anjali ने भी course join किया।\n",
"Suresh अभी सोच रहा है।\n"]
file = open("students.txt", "w")
file.writelines(lines)
file.close()
यहाँ writelines() ने पूरी list के सभी strings को एक ही बार में file में लिख दिया, जबकि write() से हमें हर line के लिए अलग से function call करनी पड़ती। इससे code भी छोटा और साफ रहता है।
3. Appending Data to Files in Hindi
कई बार हमें किसी पहले से मौजूद file का पुराना data delete किए बिना उसमें नया data add करना होता है। इसके लिए Python में "a" (append) mode use किया जाता है।
अगर हम file को "w" mode में open करेंगे, तो पहले से मौजूद पूरा data delete होकर नया data लिखा जाएगा। लेकिन "a" mode में file का पुराना data सुरक्षित रहता है और नया data उसके बाद जुड़ जाता है।
file = open("students.txt", "a")
file.write("Karan ने आज course join किया।\n")
file.close()
अगर हम students.txt file को कई बार अलग-अलग समय पर update करना चाहते हैं, जैसे रोज़ नया student add करना, तो append mode सबसे सही तरीका है, क्योंकि इससे पुराना record भी सुरक्षित रहता है और नया data भी जुड़ता जाता है।
new_students = ["Meena ने course join किया।\n", "Aakash ने course join किया।\n"]
file = open("students.txt", "a")
file.writelines(new_students)
file.close()
ऊपर दिए गए example में हमने writelines() को append mode के साथ use किया, जिससे multiple नए students का data एक साथ पुराने data के बाद जुड़ गया।
4. Difference Between write() and writelines() in Python in Hindi
write() और writelines() दोनों ही file में data लिखने के लिए use होते हैं, लेकिन इनके काम करने का तरीका थोड़ा अलग है।
यह SVG दिखा रहा है कि write() सिर्फ एक string लेता है जबकि writelines() strings की पूरी list एक साथ file में लिख देता है।
# write() से बार-बार call करना पड़ता है
file = open("data.txt", "w")
file.write("Line 1\n")
file.write("Line 2\n")
file.close()
# writelines() से एक ही बार में सब कुछ लिख जाता है
file = open("data.txt", "w")
file.writelines(["Line 1\n", "Line 2\n"])
file.close()
Simple शब्दों में कहें तो, जब हमें सिर्फ एक ही line लिखनी हो तो write() काफी होता है, लेकिन जब हमें multiple lines एक साथ लिखनी हों, तो writelines() ज्यादा efficient और clean तरीका है।