Python में File Write कैसे करें? open(), "w" Mode और write() Method
Python में File Write कैसे करें? Create Files, open() और write() Method
Python में program चलने के दौरान variables में रखा data आम तौर पर program बंद होने के बाद अपने-आप किसी text file में save नहीं हो जाता। अगर आप notes, student details, reports या किसी program का output बाद में भी इस्तेमाल करना चाहते हैं, तो आप Python की File Handling सुविधा से file बना सकते हैं और उसमें text लिख सकते हैं।
इस tutorial में हम आसान Hindi में सीखेंगे कि Python में नई file कैसे create करें, open() के "w" mode का क्या काम है, write() method कैसे इस्तेमाल होता है, multiple lines कैसे लिखें और with open() से file को safely कैसे close करें।
अगर आपने File Handling का basic workflow अभी नहीं पढ़ा है, तो पहले हमारा Python File Handling in Hindi: Core Concepts & Workflow पढ़ें। File से data पढ़ना सीखने के लिए Python File Reading in Hindi lesson भी देखें।
append(), read operations, CSV, JSON, Binary Files और File Handling के दूसरे concepts को यहाँ detail में नहीं पढ़ाया गया है।
Python में नई File Create करना
Python में file खोलने या create करने के लिए built-in open() function इस्तेमाल होता है। जब आप किसी ऐसी file को "w" mode में खोलते हैं जो अभी मौजूद नहीं है, तो Python उसे create कर देता है।
यह code my_notes.txt नाम की file बनाता है, अगर वह पहले से मौजूद नहीं है। इस example में अभी कोई text नहीं लिखा गया है, इसलिए file खाली रहेगी।
.py file और बनाई जाने वाली text file को एक ही folder में रखें, ताकि location समझना आसान रहे।open() का Write Purpose
open() का इस्तेमाल file खोलने के लिए होता है। इसके पहले argument में file का नाम और दूसरे argument में mode दिया जाता है। Write mode के लिए "w" इस्तेमाल होता है।
"file_name.txt"— जिस file को create या open करना है उसका नाम।"w"— write mode, यानी file में text लिखने के लिए।file_object— वह object जिसके जरिए हमwrite()जैसे method इस्तेमाल करेंगे।
"w" Mode क्या है?
"w" का मतलब write mode है। यह mode file में नया text लिखने के लिए इस्तेमाल होता है। इसकी एक बहुत जरूरी बात याद रखें: अगर file पहले से मौजूद है, तो "w" mode उसे खोलते ही उसका पुराना content हटा देता है। इसलिए existing file पर इसे इस्तेमाल करते समय सावधान रहें।
| स्थिति | "w" mode का व्यवहार |
|---|---|
| File मौजूद नहीं है | नई file create होती है। |
| File पहले से मौजूद है | पुराना content truncate यानी हट जाता है और file नए content के लिए तैयार होती है। |
| File में text नहीं लिखा | नई file खाली रह सकती है; existing file का पुराना content फिर भी हट सकता है। |
किसी जरूरी existing file को बिना सोचे "w" mode में न खोलें। इससे उसका पुराना content मिट सकता है। पहले test के लिए practice.txt जैसी अलग file इस्तेमाल करें।
write() Method क्या है?
write() method file में text लिखता है। यह string को file में लिखता है और लिखे गए characters की संख्या return करता है।
Program चलाने के बाद welcome.txt file में यह text होगा:
ध्यान दें कि write() अपने-आप नई line नहीं जोड़ता। अगर अगला text नई line पर चाहिए, तो string में \n लिखें।
File में Text लिखना — Step by Step
इस example में हम file खोलेंगे, text लिखेंगे और फिर file बंद करेंगे।
Terminal Output:
message.txt के अंदर:
Terminal पर दिखाई देने वाला message केवल program का output है। असली लिखी हुई सामग्री message.txt file में save होती है।
Multiple Lines लिखना
अगर file में एक से ज्यादा lines लिखनी हों, तो string में \n newline character का इस्तेमाल कर सकते हैं। \n बताता है कि अगला text नई line से शुरू होना चाहिए।
course.txt के अंदर:
ऊपर हर write() call में \n जोड़ा गया है, इसलिए अगला text नई line पर लिखा गया। आखिरी line के अंत में \n देना जरूरी नहीं है, जब तक आप अंत में newline नहीं चाहते।
Multiple write() Calls
आप एक ही file को खुला रखते हुए write() method को कई बार call कर सकते हैं। हर call text को आगे लिखती है। ध्यान रहे कि newline न देने पर text उसी line में जुड़ सकता है।
profile.txt के अंदर:
\n से नई Line बनाना
\n Python string में newline character है। File में इसे लिखने पर अगला text नई line से शुरू होता है। यह दो characters \ और n लिखने के बजाय newline character को represent करता है।
दूसरे with block में file फिर से "w" mode में खुलती है, इसलिए पहले वाला content replace हो जाता है। Final file में:
"w" mode में file को दोबारा खोलना पुराने content को मिटा सकता है। ऊपर का example जानबूझकर यह behavior दिखाता है। Real project में जरूरत के अनुसार file को एक बार खोलकर सभी lines लिखें।Existing File को "w" Mode में खोलने पर क्या होता है?
यह beginner के लिए सबसे जरूरी concept है। मान लीजिए sample.txt में पहले से यह text है:
अब यह code चलाएँ:
अब file में केवल नया content बचेगा:
यानी पुरानी दोनों lines हट गईं। ऐसा इसलिए हुआ क्योंकि "w" mode existing file को truncate करता है।
write() का Return Value
write() लिखे गए characters की संख्या return करता है। इसे variable में store करके देख सकते हैं।
Output:
क्योंकि "Hello" में 5 characters हैं। इस उदाहरण में newline नहीं है, इसलिए return value 5 है।
f.write("Hi\n") तीन characters लिखता है: H, i और newline।File को Properly Close करना
जब आप open() से file खोलते हैं, तो काम पूरा होने पर उसे बंद करना चाहिए। Manual तरीका है close() method।
f.close() file को बंद कर देता है। लेकिन अगर program में बीच में कोई error आ जाए, तो manual close छूट सकता है। इसी वजह से सामान्यतः with open() को प्राथमिकता दी जाती है।
with open() के साथ File Create और Write करना
with open() file handling का साफ और सुरक्षित तरीका है। Block पूरा होने पर file अपने-आप close हो जाती है, इसलिए अलग से close() लिखने की जरूरत नहीं रहती।
Output:
with block के अंदर file पर काम करें। Block खत्म होने के बाद file अपने-आप बंद हो जाती है। Hindi text लिखने के लिए encoding="utf-8" देना एक अच्छा अभ्यास है।
Real-world File Writing Example
मान लीजिए आप एक छोटा student profile program बना रहे हैं और student का नाम, course और website एक text file में save करना चाहते हैं।
Terminal Output:
student_profile.txt के अंदर:
इस example में variables की values को f-string के जरिए file में लिखा गया है। यह तरीका छोटे reports, notes और simple text records save करने में उपयोगी हो सकता है।
VS Code में Code कैसे Run करें?
- VS Code खोलें और एक folder open करें।
- उस folder में
file_write.pyनाम की Python file बनाएँ। - ऊपर दिया गया कोई example code paste करें।
- File save करें और Run Python File या terminal से program चलाएँ।
- उसी folder में बनी
.txtfile खोलकर उसका content check करें।
अगर आप browser में code practice करना चाहते हैं, तो हमारा Online Python Editor & Compiler in Hindi भी देख सकते हैं। ध्यान दें कि कुछ online editors में local files बनाने और देखने का तरीका आपके computer से अलग हो सकता है; real file creation को समझने के लिए VS Code अच्छा विकल्प है।
Common Mistakes
अगर important.txt पहले से मौजूद है, तो उसका पुराना content हट सकता है।
पहले practice के लिए अलग file इस्तेमाल करें और code चलाने से पहले check करें कि file में जरूरी data तो नहीं है। इस lesson में केवल write mode समझाया गया है; दूसरे modes अलग lesson में सीखें।
इससे text एक ही line में जुड़ जाएगा: First lineSecond line।
कभी-कभी file अपेक्षित folder में नहीं दिखती, क्योंकि program किसी दूसरी working directory से run हुआ होता है।
VS Code में सही project folder खोलें और terminal में current working directory जाँचें। शुरुआत में Python script और text file को एक ही folder में रखकर अभ्यास करें।
Manual open() इस्तेमाल करते समय close() भूल सकते हैं।
जहाँ संभव हो, with open() इस्तेमाल करें। इससे block पूरा होने पर file अपने-आप बंद हो जाती है।
अलग-अलग systems पर default text encoding अलग हो सकती है।
Quick Exercise 🧠
अगर notes.txt पहले से मौजूद है और आप उसे "w" mode में खोलते हैं, तो क्या होगा?
सही उत्तर: B
Final Coding Challenge
अब अपनी बारी! VS Code में my_notes.py नाम की file बनाएँ और नीचे दिए गए tasks खुद पूरा करें:
my_notes.txtनाम की नई text filewith open()और"w"mode से बनाएँ।- उसमें अपना नाम, अपना course और अपनी पसंद का एक sentence लिखें।
- कम-से-कम 5 lines लिखें।
- हर line के बाद
\nका सही इस्तेमाल करें। write()के return value को variable में store करके print करें।- Program चलाने के बाद बनी हुई text file खोलकर content verify करें।
- Bonus: एक बार file में कुछ text लिखें, फिर उसी file को दोबारा
"w"mode में खोलकर नया text लिखें। दोनों बार के content में क्या अंतर आया, उसे note करें।
Quick Revision
| Concept | Syntax / Meaning |
|---|---|
| File open/create करना | open("notes.txt", "w") |
| Write mode | "w" — file में लिखने के लिए; existing content truncate हो सकता है |
| Text लिखना | f.write("Hello") |
| New line | \n |
| Multiple write calls | एक ही open file object पर कई बार f.write() |
| Return value | लिखे गए characters की संख्या |
| Manual close | f.close() |
| Recommended pattern | with open("notes.txt", "w", encoding="utf-8") as f: |
FAQ — Python File Writing
1. Python में नई file कैसे create करें?
open("file_name.txt", "w") से ऐसी text file create हो सकती है जो अभी मौजूद नहीं है। अगर file पहले से मौजूद है, तो "w" mode उसका पुराना content हटा सकता है।
2. Python में "w" mode का क्या मतलब है?
"w" write mode है। यह file में text लिखने के लिए इस्तेमाल होता है। Existing file खोलने पर उसका content truncate हो जाता है।
3. Python का write() method क्या करता है?
write() string को file में लिखता है और लिखे गए characters की संख्या return करता है।
4. write() से नई line कैसे लिखें?
Text के अंत में \n लगाएँ, जैसे f.write("Hello\n")। इससे अगला text नई line से शुरू होगा।
5. क्या write() अपने-आप newline जोड़ता है?
नहीं। write() केवल वही string लिखता है जो आप देते हैं। नई line के लिए \n देना पड़ता है।
6. Existing file को "w" mode में खोलने पर क्या होता है?
File का पुराना content हट जाता है और file नए text के लिए तैयार हो जाती है। इसलिए जरूरी data वाली file पर इसे सावधानी से इस्तेमाल करें।
7. with open() क्यों इस्तेमाल करना चाहिए?
with open() block पूरा होने पर file को अपने-आप close कर देता है। इससे code साफ रहता है और file बंद करना भूलने की संभावना कम होती है।
8. Python में Hindi text file में कैसे लिखें?
open() में encoding="utf-8" दें। उदाहरण: with open("hindi.txt", "w", encoding="utf-8") as f:।
9. क्या write() number सीधे लिख सकता है?
write() text string लेता है। Number लिखने के लिए उसे string में बदलें, जैसे f.write(str(100))।
10. क्या File Writing का code online editor में चल सकता है?
यह editor की file-system support पर निर्भर करता है। Real local text file बनाने और उसे folder में देखने के लिए VS Code या अपने computer के terminal में अभ्यास करना बेहतर है।
Summary
Python File Writing = File खोलें या create करें →write()से text लिखें →\nसे जरूरत के अनुसार नई lines बनाएँ →with open()से file को safely close होने दें।
इस lesson में आपने Python में file create करना, open() के "w" mode का purpose, write() method, multiple lines, newline character, return value और with open() का इस्तेमाल सीखा। सबसे जरूरी बात याद रखें: "w" mode existing file का पुराना content हटा सकता है।
अब Final Coding Challenge पूरा करें और अपने code को VS Code में run करके text file check करें। अगला lesson पढ़ने के लिए नीचे दिए गए related resources इस्तेमाल करें।
Related Articles — Python Hindii
- Python File Handling in Hindi: Core Concepts & Workflow
- Python में File कैसे Read करें? Complete Guide in Hindi
- Python Exception Handling in Hindi
- Python Strings in Hindi
- Python Lists Complete Guide in Hindi
- Python For Loop in Hindi
- Python Tutorial in Hindi — Complete Roadmap
- Online Python Editor & Compiler in Hindi
Python code likho, run karo aur practice karo — directly browser mein.
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें