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:
Output:
Hello Python
Updated File Content:
Explanation
r+ से file reading और writing दोनों के लिए open हुई।
read() ने existing data पढ़ा।
seek(0) ने file position को शुरुआत में लाया।
write() ने शुरुआत से नया text लिखा।
पुराने content का शेष भाग बना रहा, क्योंकि r+ file को truncate नहीं करता।
Important Points
File पहले से मौजूद होनी चाहिए।
Reading और writing दोनों कर सकते हैं।
Existing content delete नहीं होता।
File का initial position शुरुआत में होता है।
seek() से file position बदली जा सकती है।