हिंदी

Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some error message be flashed? - Computer Science (Python)

Advertisements
Advertisements

प्रश्न

Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some error message be flashed?

टिप्पणी लिखिए

उत्तर

The main reason to close file after read and write operations, so the program does not slow down as Python makes sure that any unwritten or unsaved data is flushed off (written) to the file before it is closed. Hence, it is always advised to close the file once our work is done. Also, if the file object is re-assigned to some other file, the previous file is automatically closed.

If you keep a file open longer than it is like, you are wasting precious resources and potentially slowing down your system or making it run inefficiently.

shaalaa.com
Creating and Traversing a Text File (Data Manipulation)
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 2: File Handling in Python - Exercise [पृष्ठ ३७]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 12
अध्याय 2 File Handling in Python
Exercise | Q 4. | पृष्ठ ३७
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×