← Back to Python Course | Chapter 9: File Handling | Lesson 3 of 6

Python Files लिखना

किसी file में लिखना किसी notebook में लिखने जैसा है: आप नया पन्ना शुरू कर सकते हैं या अंत में जोड़ सकते हैं। Python आपका text save करता है ताकि program रुकने के बाद भी वह वहाँ मौजूद रहे।
Syntax
python
with open("file_name", "w") as file:  # "a" appends
    file.write("text")

File में Text लिखना

f.write(text) किसी खुली file में एक string लिखता है और लिखे गए characters की संख्या लौटाता है -- किसी file को w mode में खोलना पहले उसकी मौजूदा content को truncate कर देता है, इसलिए पहले से data वाली file में लिखना उसे पूरी तरह replace कर देगा, न कि उसमें जोड़ेगा।

उदाहरण: Writing Text to Files

python
with open("output.txt", "w") as f:
    f.write("first version")
with open("output.txt", "w") as f:
    f.write("replaced content")  # 'w' truncates first
with open("output.txt") as f:
    print(f.read())

कई Lines लिखना

f.writelines(list_of_strings) किसी भी iterable से एक साथ कई strings लिखता है, लेकिन print() के विपरीत यह उनके बीच अपने आप newlines नहीं जोड़ता -- अगर आप उन्हें अलग-अलग lines में चाहते हैं तो आपकी list की हर string को अपना trailing '\n' चाहिए होगा।

उदाहरण: Writing Multiple Lines

python
lines = ["first\n", "second\n"]  # each string needs its own newline
with open("output.txt", "w") as f:
    f.writelines(lines)  # writelines does not add newlines automatically
with open("output.txt") as f:
    print(f.read())

Files में Data जोड़ना

किसी file को w की बजाय a mode के साथ खोलना उसमें पहले से मौजूद हर चीज़ बचाकर रखता है और अंत से नई content लिखना शुरू करता है -- यह उन log files जैसी चीज़ों के लिए चाहिए मोड है जिन्हें हर बार मिटाए जाने की बजाय कई runs में entries जमा करनी चाहिए।

उदाहरण: Appending Data to Files

python
with open("log.txt", "w") as f:
    f.write("run 1\n")
with open("log.txt", "a") as f:
    f.write("run 2\n")  # appended, run 1 is preserved
with open("log.txt") as f:
    print(f.read())

Non-String Data लिखना

write() केवल strings स्वीकार करता है, इसलिए किसी number या list को सीधे लिखना TypeError raise करता है -- write() को देने से पहले आपको उसे पहले str(value) से बदलना होगा (या structured data के लिए json.dump() इस्तेमाल करना होगा)।

उदाहरण: Writing Non-String Data

python
count = 42
with open("output.txt", "w") as f:
    f.write(str(count))  # write() only accepts strings, so count must be converted
with open("output.txt") as f:
    print(f.read())

Buffers Flush करना

Performance कारणों से Python written data को disk पर वाकई भेजने से पहले memory में buffer करता है, जिसका मतलब है कि write() call करते ही data ज़रूरी नहीं कि save हो जाए -- f.flush() disk पर तुरंत लिखने के लिए मजबूर करता है, और file बंद करना (या with block से बाहर आना) अपने आप flush कर देता है।

उदाहरण: Flushing Buffers

python
with open("output.txt", "w") as f:
    f.write("buffered data")
    f.flush()  # forces the buffered data to disk immediately
    print("Flushed to disk before the block even ends")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.