Important Notice:

Append and Read Mode — a+

Append and Read Mode — a+

2 views 1 min read

Append and Read Mode — a+  :-

a+ Mode का उपयोग file को पढ़ने और उसके अंत में नया data जोड़ने के लिए किया जाता है। यदि file मौजूद नहीं है, तो Python नई file create करता है। Existing data delete नहीं होता।

English:

The a+ mode is used to open a file for both reading and appending. New data is added at the end of the file without deleting existing content.

Syntax

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

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

Rahul
Amit

Python Program:

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

file.write("\nSohan")

file.seek(0)

data = file.read()

print(data)

file.close()

Output:

Rahul
Amit
Sohan

Explanation

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

  2. write() ने file के अंत में नया data जोड़ा।

  3. seek(0) ने reading के लिए position शुरुआत में की।

  4. read() ने पूरा data पढ़ा।

  5. close() ने file बंद कर दी।

Important Points

  1. Reading और appending दोनों कर सकते हैं।

  2. Existing data delete नहीं होता।

  3. नया data file के अंत में जोड़ा जाता है।

  4. File मौजूद नहीं होने पर नई file create होती है।

  5. Reading के लिए seek(0) उपयोगी है, क्योंकि append mode में initial position file के अंत में होती है।

Related Notes