open() Function :-
open() Python का built-in function है, जिसका उपयोग file को खोलने और उससे संबंधित file object प्राप्त करने के लिए किया जाता है।
English
The open() function is a built-in Python function used to open a file and return a file object for performing file operations.
Syntax open(file, mode="r", encoding=None)
यह syntax केवल मुख्य arguments को दर्शाता है। open() में अन्य optional parameters भी उपलब्ध हैं।
मुख्य Parameters -
1. file -
File का नाम या path बताता है।
English: Specifies the filename or path of the file.
2. mode -
File खोलने का तरीका निर्धारित करता है।
English: Specifies the mode in which the file is opened.
3. encoding -
Text file को पढ़ने या लिखने के लिए character encoding निर्धारित करता है।
English: Specifies the character encoding used for text files.
उदाहरण के लिए, UTF-8 encoding:
file = open("notes.txt", "r", encoding="utf-8")
file.close()
Example: Using open() to Write and Read -
file = open("message.txt", "w", encoding="utf-8")
file.write("Welcome to Python")
file.close()
file = open("message.txt", "r", encoding="utf-8")
print(file.read())
file.close()
Output:
Welcome to Python
Important Points
open() एक built-in function है; इसे import करने की आवश्यकता नहीं होती।
यह सफलतापूर्वक file open होने पर file object return करता है।
File का mode उसके operations को निर्धारित करता है।
File के साथ काम पूरा होने पर उसे बंद करना चाहिए।
Text files के लिए encoding="utf-8" स्पष्ट रूप से देना अच्छा अभ्यास है।