Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education
🎓 100% Free · Hindi me · Beginner se Career tak

Python Tutorial in HindiiLearn Python Programming for Free

Python, AI, Data Analytics aur SQL ek hi jagah, simple Hindi me. Padho, browser me code chalao, projects banao aur career shuru karo.

main.py
# Python seekho, Hindi me naam = "Ravi" for i in range(3): print("Namaste", naam)
▸ python main.py
🐍 Python 🤖 AI & ML 📊 Data 🗄️ SQL

Chahe Student ho ya Professional, yahan aapke liye sab hai

Apna card chuno, seedha sahi jagah pahunch jaoge.

🎓

Student ho?

School ya college me ho? Python zero se seekho, notes aur cheat sheets ke saath.

  • Python basics se OOP tak
  • Notes, PDFs aur Cheat Sheets
  • Browser me practice, kuch install nahi
💼

Working Professional ho?

Kaam ke saath nayi skill jodo: data, SQL aur AI se apna kaam tez karo.

  • Pandas, NumPy, Excel, Power BI
  • SQL aur MySQL databases
  • Automation aur AI Tools
🚀

Job ya Career change?

Projects banao, interview ki taiyari karo aur sahi roadmap follow karo.

  • Real projects, portfolio ke liye
  • Python aur SQL interview questions
  • AI Engineer, Data Scientist roadmap

Python Hindii Kaise Kaam Karta Hai?

Ek hi jagah 4 layers: seekho, practice karo, skills banao aur career shuru karo. Har layer ek step hai, us par click karke seedha wahan pahuncho. Computer par drag karke 3D me ghumao bhi.

Layer 1 · Seekho

Python Basics se Advanced tak

Variables, loops, functions se OOP tak, sab simple Hindi me. Yahin se shuru karo.

Naye ho? 3 step me shuru karo

💻 Online Code Editor

Browser me hi code likho aur turant chalao

Koi software install nahi. Code likho, Run dabao aur result usi waqt dekho.

  • Python, Java, C, C++, JavaScript, HTML aur aur bhi languages
  • input(), graphs aur multi-file HTML project bhi chalte hain
  • Mobile aur computer, dono par chalta hai
main.py
naam = input("Naam batao: ") for i in range(3): print("Namaste", naam)
▸ python main.py
PythonJavaCC++JavaScriptHTML

Python se AI tak ka poora rasta

8 steps, ek ke baad ek. Aage-peeche ghumao aur apni stage se seedha shuru karo.

Step 1 🐍

Python Basics

IntroductionVariablesData TypesOperatorsInput & OutputConditionsLoops
Start Python →
Step 2 💻

Python Intermediate

ListsTuplesSetsDictionariesStringsModulesFile Handling
Continue Python →
Step 4 🗄️

SQL & Databases

SQL BasicsMySQLSELECTINSERTUPDATEDELETEJOIN
Learn SQL →
Step 5 📊

Data Analytics

NumPyPandasData CleaningData VisualizationMatplotlibExploratory Data Analysis
Learn Data Analytics →
Step 6 🤖

Machine Learning

ML BasicsSupervised & Unsupervised LearningRegressionClassificationModel EvaluationScikit-learn
Learn Machine Learning →
Step 7 🧠

Artificial Intelligence

AI FundamentalsGenerative AIAI ToolsPrompt EngineeringLLM ConceptsAI Automation
Explore AI →
Step 8 🚀

Projects & Career

Python ProjectsAI ProjectsData ProjectsPortfolioGitHubInterview PreparationCareer Roadmap
Build Your Career →
Step 1 / 8

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 भी देखें।

📌 इस Lesson का Scope: इस lesson में केवल Create / Write Files पर focus है। append(), read operations, CSV, JSON, Binary Files और File Handling के दूसरे concepts को यहाँ detail में नहीं पढ़ाया गया है।
🎥 Video में Topic समझें

Python में नई File Create करना

Python में file खोलने या create करने के लिए built-in open() function इस्तेमाल होता है। जब आप किसी ऐसी file को "w" mode में खोलते हैं जो अभी मौजूद नहीं है, तो Python उसे create कर देता है।

Example — पहली Text File बनाना
file = open("my_notes.txt", "w") file.close()

यह code my_notes.txt नाम की file बनाता है, अगर वह पहले से मौजूद नहीं है। इस example में अभी कोई text नहीं लिखा गया है, इसलिए file खाली रहेगी।

ध्यान दें: File सामान्यतः उस working directory में बनती है जहाँ से आपका Python program चल रहा है। शुरुआत में अपनी .py file और बनाई जाने वाली text file को एक ही folder में रखें, ताकि location समझना आसान रहे।

open() का Write Purpose

open() का इस्तेमाल file खोलने के लिए होता है। इसके पहले argument में file का नाम और दूसरे argument में mode दिया जाता है। Write mode के लिए "w" इस्तेमाल होता है।

Syntax
file_object = open("file_name.txt", "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 फिर भी हट सकता है।
⚠️ Important Warning

किसी जरूरी existing file को बिना सोचे "w" mode में न खोलें। इससे उसका पुराना content मिट सकता है। पहले test के लिए practice.txt जैसी अलग file इस्तेमाल करें।

write() Method क्या है?

write() method file में text लिखता है। यह string को file में लिखता है और लिखे गए characters की संख्या return करता है।

Example — File में Text लिखना
f = open("welcome.txt", "w") f.write("Welcome to Python Hindii!") f.close()

Program चलाने के बाद welcome.txt file में यह text होगा:

📄 welcome.txt
Welcome to Python Hindii!

ध्यान दें कि write() अपने-आप नई line नहीं जोड़ता। अगर अगला text नई line पर चाहिए, तो string में \n लिखें।

File में Text लिखना — Step by Step

इस example में हम file खोलेंगे, text लिखेंगे और फिर file बंद करेंगे।

Example
file = open("message.txt", "w") file.write("Namaste Ankit!") file.close() print("Text file mein likh diya gaya.")

Terminal Output:

Text file mein likh diya gaya.

message.txt के अंदर:

📄 message.txt
Namaste Ankit!

Terminal पर दिखाई देने वाला message केवल program का output है। असली लिखी हुई सामग्री message.txt file में save होती है।

Multiple Lines लिखना

अगर file में एक से ज्यादा lines लिखनी हों, तो string में \n newline character का इस्तेमाल कर सकते हैं। \n बताता है कि अगला text नई line से शुरू होना चाहिए।

Example — एक String में कई Lines
with open("course.txt", "w", encoding="utf-8") as f: f.write("Python Course\n") f.write("Lesson: File Writing\n") f.write("Website: pythonhindii.in")

course.txt के अंदर:

📄 course.txt
Python Course Lesson: File Writing Website: pythonhindii.in

ऊपर हर write() call में \n जोड़ा गया है, इसलिए अगला text नई line पर लिखा गया। आखिरी line के अंत में \n देना जरूरी नहीं है, जब तक आप अंत में newline नहीं चाहते।

Multiple write() Calls

आप एक ही file को खुला रखते हुए write() method को कई बार call कर सकते हैं। हर call text को आगे लिखती है। ध्यान रहे कि newline न देने पर text उसी line में जुड़ सकता है।

Example — कई बार write() इस्तेमाल करना
with open("profile.txt", "w", encoding="utf-8") as f: f.write("Name: Ankit\n") f.write("Course: Python\n") f.write("Platform: Python Hindii")

profile.txt के अंदर:

📄 profile.txt
Name: Ankit Course: Python Platform: Python Hindii

\n से नई Line बनाना

\n Python string में newline character है। File में इसे लिखने पर अगला text नई line से शुरू होता है। यह दो characters \ और n लिखने के बजाय newline character को represent करता है।

Example — \n के बिना और साथ में
with open("newline_demo.txt", "w", encoding="utf-8") as f: f.write("Hello ") f.write("Python") with open("newline_demo.txt", "w", encoding="utf-8") as f: f.write("Hello\n") f.write("Python")

दूसरे with block में file फिर से "w" mode में खुलती है, इसलिए पहले वाला content replace हो जाता है। Final file में:

📄 newline_demo.txt
Hello Python
याद रखें: "w" mode में file को दोबारा खोलना पुराने content को मिटा सकता है। ऊपर का example जानबूझकर यह behavior दिखाता है। Real project में जरूरत के अनुसार file को एक बार खोलकर सभी lines लिखें।

Existing File को "w" Mode में खोलने पर क्या होता है?

यह beginner के लिए सबसे जरूरी concept है। मान लीजिए sample.txt में पहले से यह text है:

📄 sample.txt — पहले का content
This is old content. Do not remove this line.

अब यह code चलाएँ:

with open("sample.txt", "w", encoding="utf-8") as f: f.write("This is new content.")

अब file में केवल नया content बचेगा:

📄 sample.txt — code चलाने के बाद
This is new content.

यानी पुरानी दोनों lines हट गईं। ऐसा इसलिए हुआ क्योंकि "w" mode existing file को truncate करता है।

write() का Return Value

write() लिखे गए characters की संख्या return करता है। इसे variable में store करके देख सकते हैं।

Example
with open("count.txt", "w", encoding="utf-8") as f: result = f.write("Hello") print(result)

Output:

5

क्योंकि "Hello" में 5 characters हैं। इस उदाहरण में newline नहीं है, इसलिए return value 5 है।

ध्यान दें: Spaces और newline characters भी लिखे जाने वाले text का हिस्सा होते हैं। उदाहरण के लिए f.write("Hi\n") तीन characters लिखता है: H, i और newline।

File को Properly Close करना

जब आप open() से file खोलते हैं, तो काम पूरा होने पर उसे बंद करना चाहिए। Manual तरीका है close() method।

Example — close() के साथ
f = open("notes.txt", "w", encoding="utf-8") f.write("Learning Python file writing.") f.close()

f.close() file को बंद कर देता है। लेकिन अगर program में बीच में कोई error आ जाए, तो manual close छूट सकता है। इसी वजह से सामान्यतः with open() को प्राथमिकता दी जाती है।

with open() के साथ File Create और Write करना

with open() file handling का साफ और सुरक्षित तरीका है। Block पूरा होने पर file अपने-आप close हो जाती है, इसलिए अलग से close() लिखने की जरूरत नहीं रहती।

Syntax
with open("file_name.txt", "w", encoding="utf-8") as f: f.write("Yahan apna text likhein")
Example — File Write और Auto Close
with open("auto_close.txt", "w", encoding="utf-8") as f: f.write("File automatically close ho jayegi.") print("File writing complete.")

Output:

File writing complete.

with block के अंदर file पर काम करें। Block खत्म होने के बाद file अपने-आप बंद हो जाती है। Hindi text लिखने के लिए encoding="utf-8" देना एक अच्छा अभ्यास है।

Real-world File Writing Example

मान लीजिए आप एक छोटा student profile program बना रहे हैं और student का नाम, course और website एक text file में save करना चाहते हैं।

Example — Student Profile Save करना
name = "Ankit Maurya" course = "Python Programming" website = "pythonhindii.in" with open("student_profile.txt", "w", encoding="utf-8") as f: f.write("Student Profile\n") f.write("----------------\n") f.write(f"Name: {name}\n") f.write(f"Course: {course}\n") f.write(f"Website: {website}\n") print("Student profile successfully saved!")

Terminal Output:

Student profile successfully saved!

student_profile.txt के अंदर:

📄 student_profile.txt
Student Profile ---------------- Name: Ankit Maurya Course: Python Programming Website: pythonhindii.in

इस example में variables की values को f-string के जरिए file में लिखा गया है। यह तरीका छोटे reports, notes और simple text records save करने में उपयोगी हो सकता है।

VS Code में Code कैसे Run करें?

  1. VS Code खोलें और एक folder open करें।
  2. उस folder में file_write.py नाम की Python file बनाएँ।
  3. ऊपर दिया गया कोई example code paste करें।
  4. File save करें और Run Python File या terminal से program चलाएँ।
  5. उसी folder में बनी .txt file खोलकर उसका content check करें।

अगर आप browser में code practice करना चाहते हैं, तो हमारा Online Python Editor & Compiler in Hindi भी देख सकते हैं। ध्यान दें कि कुछ online editors में local files बनाने और देखने का तरीका आपके computer से अलग हो सकता है; real file creation को समझने के लिए VS Code अच्छा विकल्प है।

Common Mistakes

❌ 1. "w" mode से पुराना content मिट जाना
with open("important.txt", "w") as f: f.write("New text")

अगर important.txt पहले से मौजूद है, तो उसका पुराना content हट सकता है।

✅ सही तरीका

पहले practice के लिए अलग file इस्तेमाल करें और code चलाने से पहले check करें कि file में जरूरी data तो नहीं है। इस lesson में केवल write mode समझाया गया है; दूसरे modes अलग lesson में सीखें।

❌ 2. write() के बाद नई line की उम्मीद करना
with open("demo.txt", "w") as f: f.write("First line") f.write("Second line")

इससे text एक ही line में जुड़ जाएगा: First lineSecond line।

✅ सही तरीका
with open("demo.txt", "w", encoding="utf-8") as f: f.write("First line\n") f.write("Second line")
❌ 3. File का नाम या location गलत समझना

कभी-कभी file अपेक्षित folder में नहीं दिखती, क्योंकि program किसी दूसरी working directory से run हुआ होता है।

✅ सही तरीका

VS Code में सही project folder खोलें और terminal में current working directory जाँचें। शुरुआत में Python script और text file को एक ही folder में रखकर अभ्यास करें।

❌ 4. File close करना भूल जाना

Manual open() इस्तेमाल करते समय close() भूल सकते हैं।

✅ सही तरीका

जहाँ संभव हो, with open() इस्तेमाल करें। इससे block पूरा होने पर file अपने-आप बंद हो जाती है।

❌ 5. Hindi text के लिए encoding न देना

अलग-अलग systems पर default text encoding अलग हो सकती है।

✅ सही तरीका
with open("hindi_notes.txt", "w", encoding="utf-8") as f: f.write("नमस्ते! Python सीखना शुरू करें।")

Quick Exercise 🧠

अगर notes.txt पहले से मौजूद है और आप उसे "w" mode में खोलते हैं, तो क्या होगा?

A) पुराना content हमेशा सुरक्षित रहेगा
B) पुराना content हट जाएगा और file नए text के लिए तैयार होगी
C) Python हमेशा error देगा

सही उत्तर: B

Final Coding Challenge

अब अपनी बारी! VS Code में my_notes.py नाम की file बनाएँ और नीचे दिए गए tasks खुद पूरा करें:

  1. my_notes.txt नाम की नई text file with open() और "w" mode से बनाएँ।
  2. उसमें अपना नाम, अपना course और अपनी पसंद का एक sentence लिखें।
  3. कम-से-कम 5 lines लिखें।
  4. हर line के बाद \n का सही इस्तेमाल करें।
  5. write() के return value को variable में store करके print करें।
  6. Program चलाने के बाद बनी हुई text file खोलकर content verify करें।
  7. Bonus: एक बार file में कुछ text लिखें, फिर उसी file को दोबारा "w" mode में खोलकर नया text लिखें। दोनों बार के content में क्या अंतर आया, उसे note करें।

Quick Revision

ConceptSyntax / 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 closef.close()
Recommended patternwith 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 Coding Lab

Python code likho, run karo aur practice karo — directly browser mein.