File Opening Modes in Python

File Opening Modes in Python

Before performing any operation (like read or write) on the file, first we have to open that file, for this we should see Python’s inbuilt function open(). But at the time of open, we have to specify mode, which represents the purpose of opening file.

फाइल पर कोई भी ऑपरेशन (जैसे रीड या राईट) करने से पहले, हमें सबसे पहले उस फाइल को ओपेन करना होगा, इसके लिए हमें पायथन का इनबिल्ट फंक्शन ओपेन) दिखना चाहिए। लेकिन ओपेन के समय, हमें मोड निर्दिष्ट करना होगा, जो फाइल ओपेन के उद्देश्य का प्रतिनिधित्व करता है।

Syntax:

file_object = open (file_name [, access_mode]). Below are the parameter details.

नीचे पैरामीटर विवरण हैं।

file_name: It’s a string representing the name of the file you want to access.

यह एक स्ट्रिंग है जो उस फाइल के नाम का प्रतिनिधित्व करता है जिसे आप एक्सेस करना चाहते हैं।

access_mode: It tells the program in which mode the file has to be open. It’s an optional parameter. By default, it is set to read-only <r>. In this mode, we get data in text form after reading from the file. Allowed modes in Python are:

यह प्रोग्राम को बताता है कि फाइल को किस मोड में ओपेन करना है। यह एक ऑप्शनल पैरामीटर है। डिफॉल्ट रूप से, यह रीड-ओनली <r> के लिए सेट है। इस मोड में, हमें फाइल से रीड के बाद टेक्स्ट फॉर्म में डेटा मिलता है। पायथन में अनुमत तरीकें हैं:

1.r -> (default) Open an existing file for read operation. The file pointer is positioned at the beginning of the file. If the specified file does not exist then we will get FileNotFoundError.

r -> (डिफॉल्ट) रीड ऑपरेशन के लिए एक मौजूदा फाइल ओपेन करें। फाइल पॉइंटर फाइल की शुरुआत में पोजीशन किया जाता है। यदि निर्दिष्ट फाइल मौजूद नहीं है तो हमें FileNotFoundError मिलेगा।

2.w -> Open an existing file for write operation. If the file already contains some data then it

will be overiden. If the specified file is not already available then this mode will create new file.

w-> राईट ऑपरेशन के लिए एक मौजूदा फाइल ओपेन करें। यदि फाइल में पहले से ही कुछ डेटा है तो यह ओवररीडेन होगा। यदि निर्दिष्ट फाइल पहले से उपलब्ध नहीं है, तो यह मोड नई फाइल बनाएगा।

3.a -> Open an existing file for append operation. It won’t override existing data. If the

specified file is not already available then this mode will create a new file.

a-> अपेन्ड ऑपरेशन के लिए एक मौजूदा फाइल ओपेन करें। यह मौजूदा डेटा को ओवरराइड नहीं करता है। यदि निर्दिष्ट फाइल पहले से उपलब्ध नहीं है, तो यह मोड एक नई फाइल बनाएगा।

4.r+ -> To read and write data into the file. The previous data in the file will not be deleted.

The file pointer is placed at the beginning of the file.

r+ -> फाइल में डेटा रीड और राईट के लिए। फाइल का पिछला डेटा हटाया नहीं जाएगा। फाइल पॉइंटर को फाइल की शुरुआत में रखा जाता है।

5.w+ -> To write and read data. It will override existing file.

w+ -> डेटा राईट और रीड के लिए। यह मौजूदा फाइल को ओवरराइड करेगा।

6.a+ -> To append and read data from the file. It won’t override existing data.

a+-> फाइल से डेटा को जोड़ने और पढ़ने के लिए। यह मौजूदा डेटा को ओवरराइड नहीं करता है।

7.x -> To open a file in exclusive (I don’t want to use existing file) creation mode for write operation. If the file already exists then we will get FileExistsError.

x -> एक्सक्लूसिव फाइल ओपेन करने के लिए (मैं. मौजूदा फाइल का उपयोग नहीं करना चाहता) राईट के ऑपरेशन के लिए क्रिएशन मोड। यदि फाइल पहले से मौजूद है तो हम FileExistsError प्राप्त करेंगे।

Note: All the above modes are applicable for text files only. If the above modes suffixed with ‘b’ then these represents for binary files.

नोट: उपरोक्त सभी मोड केवल टेक्स्ट फाइलों के लिए लागू हैं। यदि उपरोक्त मोड ‘b’ से सफिक्स्ड हैं तो ये बाइनरी फाइलों के लिए प्रतिनिधित्व करते हैं।

Example: rb, wb, ab, r+b, w+b, a+b, xb

Closing of a File :
एक फाइल को क्लोज करना

After completing our operations on the file, it is highly recommended to close the file. For this we have to use close() function.

फाइल पर हमारे ऑपरेशन को पूरा करने के बाद, फाइल को क्लोज करने की अत्यधिक अनुशंसा की जाती है। इसके लिए हमें क्लोज) फंक्शन का उपयोग करना होगा।

Syntax: fileObject.close ( )

Various properties of File object:
फाइल ऑब्जेक्ट के विभिन्न गुण

Once we opened a file and we got file object, we can get various details related to that file by using its properties.

एक बार जब हमने कोई फाइल ओपेन की है और हमें फाइल ऑब्जेक्ट मिल गया, तो हम उसके गुणों का उपयोग करके उस फाइल से संबंधित विभिन्न विवरण प्राप्त कर सकते हैं।

1.fileObject.name => Name of the opened file – ओपेन की गई फाइल का नाम।

2.fileObject. mode=> Mode in which the file is opened – वह मोड जिसमें फाइल ओपेन की जाती है

3.fileObject.closed=> Returns Boolean value indicates that file is closed or not –

रिटर्न बूलियन वैल्यू इंगित करता है कि फाइल क्लोज है या नहीं।

4.fileObject.readable () => Returns Boolean value indicates that whether file is readable or not.

रिटर्न बूलियन वैल्यू इंगित करता है कि फाइल रीडएबल है या नहीं ।

5.fileObject.writable () => Returns boolean value indicates that whether file is writable or not.

रिटर्न बूलियन वैल्यू इंगित करता है कि फाइल राईटएबल है या नहीं ।

Example:
fileObject = open (“Gyancs.py”, ‘w’)
print (“File Name : “, fileObject.name)
print(“File Mode : “, fileObject.mode)
print (“Is File Readable : “, fileObject.readable())
print (“Is File Writable : “, fileObject.writable ())
print(“Is File Closed : “, fileObject.closed)
fileObject.close()
print (“Is File Closed : “, fileObject.closed)

Output:
>>>
==================== RESTART: C:/Users/HP/Desktop/Gyancs.py ====================
File Name :  Gyancs.py
File Mode :  w
Is File Readable :  False
Is File Writable :  True
Is File Closed :  False
Is File Closed :  True

Note: Once the file is closed using the fileObject.close(), any attempt to use the file object will result in an error message.

नोट: एक बार fileObject.close() एक उपयोग करके फाइल क्लोज कर दिए जाने के बाद फाइल ऑब्जेक्ट का उपयोग  करने के किसी भी प्रयास का परिणाम एरर मैसेज होगा।

Example:
file = open (“Gyancs.py”, ‘r’)
print (“File Name : “, file.name)
file.close()
print (“Is File Closed : “, file.closed)
print (“File Name : “, file.read())

Output:

File Name :  Gyancs.py
Is File Closed :  True
Traceback (most recent call last):
  File “C:/Users/HP/Desktop/Gyancs.py”, line 5, in <module>
    print (“File Name : “, file.read())
ValueError: I/O operation on closed file.

Chapter wise Model Paper Link

About Me