Important Notice:

Writing onto a File

Writing onto a File

3 views 1 min read

Writing onto a File (File में Data लिखना) :-

 Python में किसी File के अंदर नया Data Store करने या पहले से मौजूद File में Data लिखने की प्रक्रिया को Writing Data to a File कहते हैं।

English: Writing Data to a File is the process of storing information in a file using a Python program.

Python में File में Data Write करने के लिए open() Function और write() Method का उपयोग किया जाता है।

Steps for Writing Data to a File

File में Data लिखने के लिए सामान्यतः निम्न Steps अपनाए जाते हैं:

  1. Open the File: open() Function की सहायता से File को Open करना।

  2. Write the Data: write() Method से Data लिखना।

  3. Close the File: close() Method से File को Close करना।

Syntax:

file = open("filename.txt", "w", encoding="utf-8")

file.write("Hello Python")

file.close()
 
Example:
 
file = open("student.txt", "w", encoding="utf-8")

file.write("Name: Rahul")
file.write("\nCourse: Python")

file.close()

File Content (student.txt):

Name: Rahul
Course: Python

Explanation:

  • open() Function File को Write Mode (w) में Open करता है।

  • write() Method File में Data लिखता है।

  • \n का उपयोग नई Line शुरू करने के लिए किया जाता है।

  • close() Method File को Close करता है।

Important Points:

  1. File में Data लिखने के लिए Write Mode (w) का उपयोग किया जाता है।

  2. यदि File मौजूद नहीं है, तो नई File Create हो जाती है।

  3. यदि File पहले से मौजूद है, तो उसका पुराना Content हट जाता है।

  4. Data लिखने के बाद File को Close करना चाहिए।

Related Notes