Advertisements
Advertisements
Question
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?
Short Note
Solution
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)
Is there an error in this question or solution?