How to Close File in Python

How to Close File in Python

After completion of work on the file, we need to properly close the file.

फाइल पर कार्य पूर्ण होने के पश्च्यात हमें फाइल को ठीक से क्लोज करने की आवश्यकता होती है।

Closing a file will free up the resources that were tied with the file and is done using Python close() method.

फाइल क्लोज करना उन संसाधनों को मुक्त कर देगा जो फाइल के साथ लगे थे और पायथन क्लोज) मेथड का उपयोग करके किया जाता है।

Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file.

पायथन में एक गैर-संग्रहित ऑब्जेक्ट को साफ करने के लिए एक गारबेज कलेक्टर है लेकिन, हमें फाइल को क्लोज करने के लिए उस पर भरोसा नहीं करना चाहिए।

Syntax: fileObject.close ()

Example:
fileObject = open (“test.txt”, encoding = ‘utf-8’)
# perform file operations fileObject.close()

This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.

यह तरीका पूरी तरह से सुरक्षित नहीं है। यदि कोई अपवाद तब होता है जब हम फाइल के साथ कुछ ऑपरेशन कर रहे होते हैं, तो कोड फाइल को क्लोज किए बिना एक्जिट कर जाता है।

A safer way is to use a try….finally block.

एक सुरक्षित तरीका एक कोशिश का उपयोग करना है … अंत में ब्लॉक करें।

try:
f = open("test.txt")
# perform file operations
finally:
f.close()

This way, we are guaranteed that the file is properly closed even if an exception is raised, causing program flow to stop.

इस तरह, हमें गारंटी दी जाती है कि फाइल को ठीक से क्लोज कर दिया गया है, भले ही कोई अपवाद रेज किया गया हो, जिससे प्रोग्राम का फ्लो बंद हो जाए।

read()

To read a file’s contents, call read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). Size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, fileObject.read() will return an empty string (“).

फाइल के कंटेंट को रीड के लिए, कॉल रीड(साइज), जो कुछ मात्रा में डेटा को रीड करता है और इसे स्ट्रिंग के रूप में (टेक्स्ट मोड में) या बाइट्स ऑब्जेक्ट (बाइनरी मोड में) देता है। साइज एक वैकल्पिक संख्यात्मक तर्क है। जब साइज छोड़ा जाता है या नकारात्मक होता है, तो फाइल की संपूर्ण सामग्री को रीड और रिटर्न कर दिया जाएगा, यदि आपकी मशीन आपकी मेमोरी से दोगुनी बड़ी है तो यह आपकी समस्या है। अन्यथा, अधिकांश आकार के बाइट्स रीड किए जाते हैं और रिटर्न आ जाते हैं। यदि फाइल का इंड हो गया है, तो fileObject.read() एक empty string (‘) रिटर्न करेगा।

  • read () => To read total data from the file – फाइल से कुल डेटा रीड के लिए

  • read (n) => To read ‘n’ characters from the file – फाइल से ‘n’ कैरेक्टर रीड के लिए

Example 1: To read total data from the given specified file.

उदाहरण 1: दी गई निर्दिष्ट फाइल से कुल डेटा रीड करने के लिए।

file = open(“gyancs.txt”, ‘r’)
data = file.read()
print (data)
file.close()

Output:
Be a Master
Be a Creator
Don’t follow Anyone, Learn from everyone
Jay Ho…

Example 2: To read only first 10 characters from given specified file

उदाहरण 2: दी गई निर्दिष्ट फाइल से केवल पहले 10 कैरेक्टर रीड-ओनली के लिए

file = open (“gyancs.txt”, ‘r’)
data = file.read (10)
print (data)
file.close()

Output:
Be a Maste

readline()

Readline() function reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if readline() returns an empty string, the end of the file has been reached, while a blank line is represented by ‘\n’, a string containing only a single newline.

रीडलाईन() फंक्शन फाइल से एक पंक्ति रीड करता है, एक नई लाइन कैरेक्टर(\n) स्ट्रिंग के अंत में छोड़ दी गई है, और – फाइल की अंतिम पंक्ति पर केवल तभी छोड़ा जाता है यदि फाइल किसी नई लाईन में समाप्त नहीं होती है। यह रिटर्न वैल्यू को अस्पष्ट बनाता है, यदि रीडलाइन) एक खाली स्ट्रिंग रिटर्न करता है, तो फाइल के अंत में पहुँच गया है, जबकि एक रिक्त लाईन ‘\n’ द्वारा दर्शाई गई है, एक स्ट्रिंग जिसमें केवल एक नई लाईन है।

  • readline() => To read only one line – केवल एक पंक्ति रीड के लिए।

  • readlines() => to read all lines into a list – एक सूची में सभी लाइनों को रीड के लिए।

Example 3: To read data line by line from given specified file.

उदाहरण 3: निर्दिष्ट फाइल से लाइन द्वारा डेटा लाइन रीड के लिए।

file = open(“gyancs.txt”, ‘r’)
linel = file.readline ()
line2 = file.readline ()
line3 = file.readline ()
file.close ()
print (linel)
print (line2)
print (line3)

Output:
Be a Master
Be a Creator
Don’t follow Anyone, Learn from everyone

Chapter wise Model Paper Link

About Me