Important Notice:

Write and Read Mode — w+

Write and Read Mode — w+

3 views 1 min read

Write and Read Mode — w+  :-

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

English:

The w+ mode is used to open a file for both writing and reading. If the file already exists, its previous content is truncated. If it does not exist, a new file is created.

Syntax

file = open("filename.txt", "w+")

Example

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

file.write("Hello Python")

file.seek(0)

data = file.read()

print(data)

file.close()

Output:

Hello Python Explanation

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

  2. write() ने file में text लिखा।

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

  4. read() ने लिखा हुआ data पढ़ा।

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

Important Points

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

  2. Existing file का पुराना content delete हो जाता है।

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

  4. Initial file position शुरुआत में होती है।

  5. Data लिखने के बाद पढ़ने के लिए seek(0) का उपयोग किया जा सकता है।

Related Notes