CBSE Class 12 Computer Science Question 85 of 136

Data File Handling — Question 24

Back to all questions
24
Question

Question 24

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?

Answer

It is a good practice to close a file once we are done with the read and write operations. When we close a file, the system frees the memory allocated to it. Python ensures that any unwritten or unsaved data is flushed (written) to the file before it is closed. Therefore, it is always advised to close the file once our work is done. If we do not close file explicitly it will close automatically later when it's no longer in use or when the program terminates, without displaying any error message.