Important Notice:

Read and Write Mode — r+

Read and Write Mode — r+

3 views 1 min read

Read and Write Mode — r+ :-

r+ Mode का उपयोग existing file को पढ़ने और उसमें data लिखने दोनों के लिए किया जाता है। इस mode में file पहले से मौजूद होनी चाहिए। यह file का पुराना content delete नहीं करता।

English:

The r+ mode is used to open an existing file for both reading and writing. The file must already exist, and its previous content is not truncated.

Syntax file = open("filename.txt", "r+") Example

मान लीजिए student.txt में यह data है:

Hello Python

Python Program:

file = open("student.txt", "r+")

data = file.read()
print(data)

file.seek(0)
file.write("Welcome")

file.close()

Output:

Hello Python

Updated File Content:

Welcome Python

Explanation

  1. r+ से file reading और writing दोनों के लिए open हुई।

  2. read() ने existing data पढ़ा।

  3. seek(0) ने file position को शुरुआत में लाया।

  4. write() ने शुरुआत से नया text लिखा।

  5. पुराने content का शेष भाग बना रहा, क्योंकि r+ file को truncate नहीं करता।

Important Points

  1. File पहले से मौजूद होनी चाहिए।

  2. Reading और writing दोनों कर सकते हैं।

  3. Existing content delete नहीं होता।

  4. File का initial position शुरुआत में होता है।

  5. seek() से file position बदली जा सकती है।

Related Notes